May 19 codewar

·

1 min read

Again time is not my friend sometimes hahahaha. I think I have enough time to code and write before it's another day. But here we are.

Todays codewar was counting the elements in an array that were true and returning the sum.

Pseudo Code: /* The present sheep should be accounted for the amount of trues should be returned filtered out from the false returned into a number

filter throught the array with .filter method the parameter will be true and I also have to check for null or undefiened values then add a conditional that will return true if it equals to true and it will add it using the .length condition */

My first attempt:

function countSheeps(arrayOfSheep) {
    return arrayOfSheep.filter(n => n === true)
}

It did not work because it was not returning the final sum of those elements that were true. I googled it asking for how to return the sum of elements after a filter method. Found a good explanation that I needed .length to get the count.

function countSheeps(arrayOfSheep) {
    const presentSheep = arrayOfSheep.filter(n => {
      if (n === true){
        return true;
      }
        return false

    }).length
  return presentSheep
}

and it passed :)