2

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.

3
  • using shift() doesn't make sense unless you were looping through array Commented May 27, 2017 at 15:11
  • 1
    Look at array.reduce. Would be brilliant for this. Commented May 27, 2017 at 15:14
  • @charlietfl, the problem is that shift method modify the original array like in my answer. Commented May 27, 2017 at 15:17

3 Answers 3

1

You can use filter method for a more smart solution.

filter method creates a new array by appying a provided callback function for every item in the array.

If you are using ES6, try arrow functions.

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];
function count(arr, value) {
   if(value==0)
    return arr.filter(a=>a==0).length;
   return arr.filter(a=>a>=value).length;  
}
console.log(count(container, 2));
console.log(count(container, 0));

What's wrong with your function ?

The problem is that shift method modify the original array and that's the reason you got 0 result when you call the function with value 0.Please have a look to the below snippet.

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];
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(container);
console.log(count(container, 0));

To solve this you have to use a copy in order to invoke the count function everytime you need.

Sign up to request clarification or add additional context in comments.

2 Comments

count all value equal or greater you probably want to use a >= value
Thanks! I think this is the quickest way to do that I need, was so simple
1

Using reduce, pretty much a one liner.

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];

let zeroes = 0;

let result = container.reduce(function(a,b){
  if (!b)
  {
    zeroes++;
  }
  return a + b;
}, 0);

console.log(result, zeroes);

2 Comments

Why not start the reduce with an object, one property for the zero count, another for the value count. Then you don't need a variable global to the reduce.
@ste2425 I guess that could work too. Just depends how they want to use it.
1

You could check value, which function should be use.

function count(arr, value) {
    return arr.filter(value ? a => a >= value : a => a === 0).length;
}

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];

console.log(count(container, 2));
console.log(count(container, 0));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.