Currently working through codewars and I seem to be running into the same issue over and over again, I cannot return the inner functionality. The below is the current example but it happens every time I try something.
The question posed is:
Consider an array of sheep where some sheep may be missing from their place. We need a function that counts the number of sheep present in the array (true means present).
For example,
arrayOfSheep = [true, true, true, false,
true, true, true, true ,
true, false, true, false,
true, false, false, true ,
true, true, true, true ,
false, false, true, true];
So I have it working globally like the below:
let count = [];
for(let i = 0 ; i < arrayOfSheep.length ; i++) {
if(arrayOfSheep[i] == true) {
count ++;
}
}
console.log(count);
which returns 17
in the terminal; the number of instances of true
within arrayOfSheep
. Great.
I know that to return the functionality i should use the return
keyword.
This is the code which doesn't produce anything to the terminal:
function countSheeps(arrayOfSheep) {
let count = [];
for(i = 0 ; i < arrayOfSheep.length ; i ++) {
if(arrayOfSheep[i] == true) {
return count ++;
}
}
};
console.log(count);
it should just return the integer 17
. But instead I get the error message
ReferenceError: count is not defined
What really obvious thing am I missing, I KNOW i am going to kick myself when someone is kind enough to point it out...
Thanks in advance.