-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbird-watcher.js
48 lines (44 loc) · 1.19 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
// @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 total = 0;
for (let i = 0; i < birdsPerDay.length; i++) {
total += birdsPerDay[i];
}
return total;
}
/**
* 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) {
const weekIndex = (week - 1) * 7;
return totalBirdCount(birdsPerDay.slice(weekIndex, weekIndex + 7));
}
/**
* 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.splice(i, 1, birdsPerDay[i] + 1)
}
}
return birdsPerDay
}