-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountSheepsArray.Js
More file actions
36 lines (27 loc) · 1.47 KB
/
countSheepsArray.Js
File metadata and controls
36 lines (27 loc) · 1.47 KB
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
26
27
28
29
30
31
32
33
34
35
36
// 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
function countSheeps(arrayOfSheep) {
return arrayOfSheep.filter(sheep => sheep === true).length;
}
//changed from sheep
const sheeps = [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(sheeps)); // Output: 17
// While includes alone isn't suited for counting values, filter works well here because it creates a new array containing only true values, and then .length gives the count.