string conversion to numbers to avoid dupes [duplicate] - javascript

This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
How to convert a string to an integer in JavaScript
(32 answers)
Closed 1 year ago.
function removeDups(arr) {
let x = {};
for (let i=0; i<arr.length; i++) {
if(!x[arr[i]]) {
x[arr[i]] = true;
}
}
let result = Object.keys(x);
for(let i=0; i<result.length; i++) {
if(typeof result[i] == Number) {
result[i] = parseInt(result[i]);
}
}
return result;
}
removeDups([1, 0, 1, 0]);
//➞ [1, 0]
Hey guys trying to return [1,0] like the problem states but I keep returning ['1','0']. I'm trying to convert these values to numbers and not having much luck. Any help?

Related

ways to check for empty array in a conditional in JS [duplicate]

This question already has answers here:
Empty array does not equal itself in Javascript?
(4 answers)
Closed 1 year ago.
I'm wondering why my conditional statement where I'm trying to sort out the edge case of the input array being empty doesn't seem to work. With my current test case, the result I get is NaN (no idea why). If I change the conditional to if (array.length === 0), then my code functions the way I want it to. Isn't [] the correct representation of an empty array though?
function getAvg(array) {
if (array === []) {
return 0;
}
var sum = 0;
var average = 0;
var length = array.length;
for (var i = 0; i < length; i++) {
sum += array[i];
}
average = sum / length;
return average
}
var input = [];
var result = getAvg(input);
console.log('should be 0:', result);
You can check if length of array is greater than 0\
if(arr.length>0){}

How do I get the console to output my number? [duplicate]

This question already has answers here:
How to call a function in Javascript [duplicate]
(4 answers)
Closed 2 years ago.
I'm just a beginner in JavaScript and I can't solve a problem with a function. I need the code to cycle through an array of let's say the numbers ["3", "5", "7", "4", "9"] and console.log only the highest number in the array. This is the code I'm working with and I had tried many variations to get the function to console.log the number 9 in this case. What is the proper way to insert an array here and I need to switch the return max to console.log(max). Until now I just had an error or the console wont show anything, I need an answer at this point to get me through it.
function findMax(arr){
let max = arr[0];
for(let i = 1; i < arr.length; i++){
if(arr[i] > max){
max = arr[i];
}
}
return max;
}
function findMax(arr){
let max = arr[0];
for(let i = 1; i < arr.length; i++){
if(arr[i] > max){
max = arr[i];
}
}
return max;
}
console.log(findMax(['1','2','3','4','5']))
console.log(findMax([1,2,3,4,5]))

Does splicing of an array reduce the value of array.length? [duplicate]

This question already has answers here:
Remove items from array with splice in for loop [duplicate]
(5 answers)
Closed 3 years ago.
function filter_list(l) {
for (var i = 0; i < l.length; i++) {
if (typeof(l[i]) === 'string') {
l.splice(i, 1);
}
}
return l;
}
console.log(filter_list([1, 2, 'a', 'b']));
When element 2 (index starts with 0) is spliced why doesn't the length of the array in the for loop change to 3? The last element should not be processed but it is processed.
Does splicing of an array reduce the value of array.length?
Yes, because splice method modifies the array inplace
The solution could be using a while loop.
function filter_list(l) {
i = l.length;
while (i--) {
if (typeof(l[i]) === 'string') {
l.splice(i, 1);
}
}
return l;
}
console.log(filter_list([1, 2, 'a', 'b']));

How to save java script data from a loop to an array? [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Returning only certain properties from an array of objects in Javascript [duplicate]
(5 answers)
Javascript Array of objects get single value [duplicate]
(3 answers)
Construct an array of elements from an array of objects? [duplicate]
(2 answers)
Closed 4 years ago.
How to save javascript data from a loop to an array?
for (i = 0; i < jsonData.Data.Positions.length; i++) {
var h = jsonData.Data.Positions[i].Oid;
}
Insert the data in the array using push
var arr=[];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
var h = jsonData.Data.Positions[i].Oid;
arr.push(h);
}
var data = [];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
var h = jsonData.Data.Positions[i].Oid;
data.push(h)
}
//OR
var data = jsonData.Data.Positions.map(item => item.Oid);
Your variable jsonData.Data.Positions is probably already an array.
Use .push() method to add values to array.
var h=[];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
h.push(jsonData.Data.Positions[i].Oid);
}
console.log(h);
You can do it within a loop:
var array = []
for (i = 0; i < jsonData.Data.Positions.length; i++) {
array.push(jsonData.Data.Positions[i].Oid);
}
Or in a more functional-way:
var array = jsonData.Data.Positions.map(p => p.Oid)
map instead of for loop
var h = jsonData.Data.Positions.map(function (x) { return x.0id });

calculate the sum of a list of lists [duplicate]

This question already has answers here:
Javascript unlimited nested array handling
(6 answers)
Closed 8 years ago.
I found this code on http://games.usvsth3m.com/javascript-under-pressure/ and I reach the 5th problem which requires to estimate the sum of all the integer in the list. The list may contains nested lists. So for a list [1,2,3,4,5] or [[1,2,3],4,5] the code below works but for a list [[[[[1]]]],2,3,4] does not. I try a lot of hours and I do not know to solve it. I need a hit plz.
function arraySum(i) {
var sum =0;
for (var id =0; id<i.length;id++){
if(typeof i[id]==='object'){
var ar = i[id];
for (var dd =0; dd<ar.length;dd++ ){
if(typeof ar[dd]==='number'){
sum+=parseInt(ar[dd]);
}
}
}
else
if(typeof i[id]==='number'){
sum+=parseInt(i[id]);
}
}
return sum;
}
use recursion.
var arr = [1, 2, [3, 4, [[[5]]]]];
num = 0;
function loop (arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] instanceof Array) {
loop(arr[i]);
} else {
num += parseInt(arr[i]);
}
}
}
loop(arr);
console.log(num);
fiddle - http://jsfiddle.net/9j1hcx4x/

Categories

Resources