0

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.

user2342558
  • 5,567
  • 5
  • 33
  • 54
  • 1) You're not calling the function. 2) You're initializing `count` as an array, but incrementing it like a number. – Robby Cornelissen Oct 02 '19 at 10:13
  • 3. *"I know that to return the functionality i should use the return keyword."* True, but not where you have it. Use `return count;` at the **end** of the function. If you do `return` from within your loop, you exit the loop (and the function) before the loop completes. 4. Function *declarations* don't have `;` after them, only function expressions. [More on the differences](https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname/22173438#22173438)... – T.J. Crowder Oct 02 '19 at 10:14
  • 1
    5) The plural of sheep is sheep, not sheeps :) – Robby Cornelissen Oct 02 '19 at 10:15
  • Well the variables inside of an function live as long as the function self, that means when you try to console.log the count variable is already destroyed – bill.gates Oct 02 '19 at 10:17
  • as an aside: don't write `if(arrayOfSheep[i] == true)` you don't have to compare true to true. `if(arrayOfSheep[i])` is sufficient. – ohno Oct 02 '19 at 10:56

2 Answers2

0
  1. The variable count is defined inside the countSheeps function, so it's not accessible outside of that function. Plus, you didn't call the function in your example code.

  2. You should increase the count instead of returning it since a for loop doesn't return anything.

  3. A function can return some values. We're returning the final output of the variable count here so it can be displayed in the console.log().

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

function countSheeps(arrayOfSheep) {
  let count = 0;
  for (i = 0; i < arrayOfSheep.length; i++) {
    if (arrayOfSheep[i] == true) {
      count++;
    }
  }

  return count;
}

console.log(countSheeps(arrayOfSheep));
Joshua
  • 3,055
  • 3
  • 22
  • 37
  • 1
    Code dumps are not *useful* answers. Say what you did, and why. (From the outset, rather than via an edit later.) – T.J. Crowder Oct 02 '19 at 10:17
0

Let is Block scope variable Variables declared with the let keyword can have Block Scope.

Variables declared inside a block {} can not be accessed from outside the block if you want to use count from outside declare it as global

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];
function countSheeps(arrayOfSheep) {
    let count = [];
     for(i = 0 ; i < arrayOfSheep.length ; i ++) {
       if(arrayOfSheep[i] == true) {
        return count ++;
       }
     }
   console.log(count);
   };