-
Notifications
You must be signed in to change notification settings - Fork 0
/
Counting_sheep.js
35 lines (24 loc) · 875 Bytes
/
Counting_sheep.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
26
27
28
29
30
31
32
33
34
35
/* Task :
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. */
/* --------------------------------------------------------------------------------------------------- */
const counting = (arr) => {
var sum = 0
for (let i = 0 ; i <arr.length ; i++){
arr[i] === true ? sum = sum + 1 : null
}
return sum
}
console.log(counting([true, true, true, false,
true, true, true, true ,
true, false, true, false,
true, false, false, true ,
true, true, true ,
false, false, true, true]))