15- Exercise 6- Count Truthy

Hello I am trying to solve the exercise here but the code is not working so can anybody tell me whats the problem

Here is my solution

const falsei = ["", 0, false, null, undefined];

console.log(countTruthy(falsei));

function countTruthy(arr) {

let count = 0;

for (let value of arr) {

if (value) count++;

}
return count;
}

  1. the parameter and argument do not match.
  2. you have a truthy statement of “value”, and it should be a falsey statement of !value.
const falsei = ["", 0, false, null, undefined];

console.log(countTruthy(falsei));

function countTruthy(falsei) {

    let count = 0;

    for (let value of falsei) {

        if (!value) count++;

    }
    return count;
}
1 Like

1 why the parameter and the argument should match ?

2 if I had a truthy statement of “value”, and it should be a falsey statement of !value. it will count only the falsy values

1 Like

Why do you think it’s not working?

1 Like

That is why I am asking here

Please clarify “not working”. Do you get an error message? Do you get other results than you expected?

The code is working for me so you need to explain your problem with it.

1 Like
const falsei = ["", 0, false, null, undefined]; //creating a variable.

console.log(countTruthy(falsei));  // console.log is writing to the console. (countTruthy(falsei) is executing the function (to the console).

function countTruthy(falsei) { // calling the parameter, located outside of the function.

    let count = 0;

    for (let value of falsei) { //since the variable was called, it can be used inside the function.

        if (!value) count++;

    }
    return count;
}

You may need to find outside resources to explain ‘things’ better. Such as argument/parameter. Which Mosh did explain in one of his videos. Also, truthy/falsey. And many other things. You will not be able to watch the video once and ‘take it all in’. Review… review… review! Redo the quizzes… redo the quizzes… redo the quizzes. Understand what the program is trying to do.

1 Like

special thanks for your advice I will do I really appreciated

I found it is working now thank you SAM