Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions solutions/javascript/bird-watcher/1/bird-watcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// @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 startIndex = (week - 1) * 7;
const endIndex = startIndex + 7 ;
let total = 0;
for ( let i = startIndex; i < endIndex; i++){
total += birdsPerDay[i]
}
return total;
}

/**
* Fixes the counting mistake by increasing the bird count
* by one for every second day.
*
* @param {number[]} birdsPerDay
* @returns {void} should not return anything
*/
export function fixBirdCountLog(birdsPerDay) {

for (let i = 0; i < birdsPerDay.length; i += 2) {
birdsPerDay[i] += 1;
}


}