Stuck on the bird watcher exercise

I don’t know what’s wrong with my code, I attempt everything and couldn’t pass

// @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) {
  //starts with 0
  let count = 0;
  for (let i = 0; i < birdsPerDay.length; i++) {
    count += birdsPerDay[i]; //adds the number of birds per day to the count
  }
  return count; //-> return of the function
}
/**
 * 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;
  //the same as the totalBirdCount function, but only for a specific week
  for (let i = 7 * (week - 1); i < 7 * week; i++) { // the week have 7 days, so we need to multiply the week number by 7 and -1 to get the first day of the week
    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) {
  // inicialize an empty array to store the correct data
  let everyTwoDays = [];
  //then Loop through the array and add 1 to every second day
  for (let i = 0; i < birdsPerDay.length; i++) {
    //if the index of the element is an odd number, increment by one
    if (i % 2 !== 0) {
      everyTwoDays.push(birdsPerDay[i] + 1);
    }
    //else just push the element to the new array
    else {
      everyTwoDays.push(birdsPerDay[i]);

      return everyTwoDays;
    }
  }
}

What happens when you run the tests? Is there an error message?

Hello! thank you for askin

SyntaxError: <solution>/bird-watcher.js: Identifier 'totalBirdCount' has already been declared. (61:16)

      59 | }
      60 |
    > 61 | export function totalBirdCount(birdsPerDay) {
         |                 ^
      62 |   let count = 0;
      63 |   for (let i = 0; i < birdsPerDay.length; i++) {
      64 |     console.log(`Adding ${birdsPerDay[i]} to ${count}`);

it’s really hard to know what’s happening :(

This suggests you’re defining the function on line 61. The code you shared shows the function defined on line 13.

Are you sharing your complete code? Is there additional code somewhere?

This is a shot in the dark since i’m also stuck on this problem but i think there are two problems with this solution.

  1. We must add 1 bird starting at index 0
  2. We mustn’t create a new array

Making the if statement (i % 2 === 0) will correct the first issue. the second issue, i’m not quite sure how to solve yet.

export function fixBirdCountLog(birdsPerDay) {
  let correctBirdsPerDay = [];
  for (let i = 0; i < birdsPerDay.length; i++) {
    if (i % 2 === 0) {
      correctBirdsPerDay.push(birdsPerDay[i] + 1);
    } else {
      correctBirdsPerDay.push(birdsPerDay[i]);
    }
  }
  return correctBirdsPerDay;
}

This code passes all but one test;

CODE RUN
const birdsPerDay = [2, 0, 1, 4, 1, 3, 0];
expect(Object.is(fixBirdCountLog(birdsPerDay), birdsPerDay)).toBe(true);

TEST FAILURE
Error: expect(received).toBe(expected) // Object.is equality

Expected: true
Received: false

Any suggestions on how to modify the array parameter without returning it as a new array or creating an infinite loop?

Your return is nested in the else statement.

It’s been four months. Something tells me this user is no longer working on this exercise :wink:

@psychederik As Isaac said, this is a pretty old thread. If you open a new one here, you’re more likely to get eyeballs on it from the community :slight_smile: