-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbird-watcher.js
50 lines (47 loc) · 1.15 KB
/
bird-watcher.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// @ts-check
//
// The line above enables type checking for this file. Various IDEs interpret
// the @ts-check directive. It will give you helpful autocompletion when
// implementing this exercise.
/**
* Calculates the total bird count.
*
* @param {number[]} birdsPerDay
* @returns {number} total bird count
*/
export function totalBirdCount(birdsPerDay) {
let sum = 0;
for (let i = 0; i < birdsPerDay.length; i++) {
sum += birdsPerDay[i];
}
return sum;
}
/**
* Calculates the total number of birds seen in a specific week.
*
* @param {number[]} birdsPerDay
* @param {number} week
* @returns {number} birds counted in the given week
*/
export function birdsInWeek(birdsPerDay, week) {
let sum = 0;
for (let i = (week-1)*7; i < week*7; i++) {
sum += birdsPerDay[i];
}
return sum;
}
/**
* Fixes the counting mistake by increasing the bird count
* by one for every second day.
*
* @param {number[]} birdsPerDay
* @returns {number[]} corrected bird count data
*/
export function fixBirdCountLog(birdsPerDay) {
for (let i = 0; i < birdsPerDay.length; i++) {
if (i % 2 == 0) {
birdsPerDay[i]++;
}
}
return birdsPerDay;
}