Trying to iterate over array

Blue

So I am trying to tell whether the sheep is present or not (true), and if it is (false) to pop it out of the array.

I am new to CodeWars, and to be honest.. I haven't been really working with for loops that much lately.

Question: Consider an array/list 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).

My code :

function countSheeps(arrayOfSheep) {
  for (i = 0; i < arrayOfSheep.length; i++ ) {
    console.log(arrayOfSheep[i])

    sheep = 0
    if (arrayOfSheep === true) {
      return sheep++
    } else {
      arrayOfSheep.pop()
    }
  }
  console.log(sheep)
}

The test:

var array1 = [true,  true,  true,  false,
              true,  true,  true,  true ,
              true,  false, true,  false,
              true,  false, false, true ,
              true,  true,  true,  true ,
              false, false, true,  true ];

Test.expect(countSheeps(array1) == 17, "There are 17 sheeps in total")
Jason Goemaat

Seems like a lot of work required to fix this, let's see the problems:

function countSheeps(arrayOfSheep) {
  for (i = 0; i < arrayOfSheep.length; i++ ) {
    console.log(arrayOfSheep[i])

    sheep = 0
    if (arrayOfSheep === true) {
      return sheep++
    } else {
      arrayOfSheep.pop()
    }
  }
  console.log(sheep)
}
    if (arrayOfSheep === true) {

arrayOfSheep is an array. It looks like you're looping through the array and would want to use i as an index, so that would be arrayOfSheep[i].

      return sheep++

This would take the value of sheep increment sheep to 1 and return the original value 0 immediately, skipping the rest of the array. Your function would return 0 the first time it found a true value.

arrayOfSheep.pop()

This removes the last element from the array. Something you certainly don't want to do before you check it...

sheep = 0

It seems like you want sheep to store the count of 'true' values in the array. Resetting it to 0 every time through your array would mean you would only count the final 'true' value.

  console.log(sheep)
}

There's no return statement here. If you make it to the end of the array, your function will return undefined by default. Running tests on codewars I'm guessing you're seeing either 0 or undefined as your result.

Keeping close to your code and making it work would be something like this:

function countSheeps(arrayOfSheep) {
  let sheep = 0; // set value once before counting
  for (let i = 0; i < arrayOfSheep.length; i++ ) {
    if (arrayOfSheep === true) {
      sheep++; // increment value of sheep variable
    }
  }
  return sheep; // return the cound in the sheep variable as your function return value
}

There are faster methods in newer javascript though. You could filter the sheep for true values and return the length of the result:

function countSheep(arrayOfSheep) {
  // trueValues will only havet he true values from arrayOfSheep
  let trueValues = arrayOfSheep.filter(sheep => sheep === true);
  return trueValues.length;
}

Another way seems perfect for reduce which operates on each element of the array:

function countSheep(arrayOfSheep) {
  return arrayOfSheep.reduce((acc, sheep) => sheep ? acc + 1 : acc, 0);
}

This starts with the 0 value and calls the function for each element in the array. If the value is true, it returns acc + 1, otherwise it returns acc without changes. So the first time through the loop acc will be 0 and sheep will be the first value in the array. If that is true, it will return 1, otherwise it will return 0. The next time through the array acc will be that return value (0 or 1) and sheep will be the next element in the array.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related