-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountSheepsArray.Js
25 lines (20 loc) · 986 Bytes
/
countSheepsArray.Js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 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.
function countSheeps(arrayOfSheep) {
let count = 0; // Start with count 0
for (let i = 0; i < arrayOfSheep.length; i++) { // Go through each sheep
if (arrayOfSheep[i] === true) { // If the sheep is present (true)
count += 1; // Increase the count by 1
}
}
return count; // Return the total count
}
const sheep = [true, true, true, false, true, true, true, true, true, false, true, false, true, false, false, true, true, true, true, false, false, true, true];
console.log(countSheeps(sheep)); // Output: 17