-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathCounting_Sheep.js
More file actions
29 lines (24 loc) · 695 Bytes
/
Counting_Sheep.js
File metadata and controls
29 lines (24 loc) · 695 Bytes
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
/*
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.
Hint: Don't forget to check for bad values like null/undefined
*/
//Answer//
function countSheeps(arrayOfSheep) {
let total = 0;
const sleep = array => {
for (let i = 0; i < arrayOfSheep.length; i++) {
if (arrayOfSheep[i] == 1) {total++}
}
}
sleep()
return (total);
}