Counting Sheep~

chris0205.eth·2022년 2월 11일
0

Codewars

목록 보기
1/5
post-thumbnail

Prob

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).

For example,

[true, true, true, false,
true, true, true, true ,
true, false, true, false,
true, false, false, true ,
true, true, true, true ,
false, false, true, true]

The correct answer would be 17.

Answr

function countSheeps(arrayOfSheep) {
  let lsheep = arrayOfSheep.length;
  let nsheep=0;
  
  for(let i=0;i<lsheep;i++){
    if (arrayOfSheep[i]==true){
      nsheep+=1;
    } else{
      continue;
    }
  }
  
  return nsheep;
}
function countSheeps(arrayOfSheep) {
  var num = 0;
  
  for(var i = 0; i < arrayOfSheep.length; i++)
    if(arrayOfSheep[i] == true)
      num++;
      
  return num;
}
function countSheeps(arrayOfSheeps) {
  return arrayOfSheeps.filter(Boolean).length;
}
profile
long life, long goal

0개의 댓글