is 2 days that I try but with no luck! Lol
My noob question is
I've a container
var container = [1.29,1.30,1.20,1.21,1.95,4.19,1.53,4.11,1.06,84.64,0,23.1,1,22,1.22];
So, I need to find a fast solution to look inside this container and count value, I've to respect only 2 rules If the value I have to count is 0, I need to know exactly how many 0 there's in the container If the value is not a 0, I need to count all value equal or greater that this value
So I tried this 2 ways, but in both case I got the wrong result for the 0 value.
function count(arr, value) {
if (arr.length === 1) {
return arr[0] === value ? 1 : 0;
} else {
if (value === 0) {
return (arr.shift() === value ? 1 : 0) + count(arr, value);
} else {
return (arr.shift() >= value ? 1 : 0) + count(arr, value);
}
}
}
console.log(count(container, 2));
console.log(count(container, 0));
My expectation is to find all value >= of 2 and all value === 0 But I got right result for 2 and wrong for 0
Same with this 2 variation of the same function
function isBigEnough(value) {
return function(element, index, array) {
if (value === 0) {
return (element = value);
} else {
return (element >= value);
}
}
}
function isBigEnough(value) {
if (value === 0) {
return function(element, index, array) {
return (element = value);
}
} else {
return function(element, index, array) {
return (element >= value);
}
}
}
Every time I got 0 result for the 0 value, but there's 1 in container.
Any idea? And what is the more fast aproach?
Many thanks and have a nice day.
shift()doesn't make sense unless you were looping through arrayshiftmethod modify the original array like in my answer.