Created
February 17, 2023 11:16
-
-
Save lubien/7e7bb28221185a83ba1c6dc2992d2236 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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. | |
const WEEK_DAYS = 7 | |
/** | |
* 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 | |
// return birdsPerDay.reduce((x, y) => x + y, 0) | |
} | |
/** | |
* 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 total = 0 | |
const start = (week - 1) * WEEK_DAYS | |
for (let i = start; i < start + WEEK_DAYS; i++) { | |
total += birdsPerDay[i] | |
} | |
return total | |
// return totalBirdCount(birdsPerDay.slice(start, start + WEEK_DAYS)) | |
} | |
/** | |
* 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] = birdsPerDay[i] + 1 | |
} | |
} | |
return birdsPerDay | |
// return birdsPerDay.map((b, i) => i % 2 === 0 ? b + 1 : b) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment