-1

I'm trying to figure out an exercise from Exercism's Javascript track. It is an exercise using for loops called Bird Watcher.

The instructions say to initialize a function called birdsInWeek that takes two arguments. First is an array 'birdsPerDay', and the second is 'week'. Given the birdsPerDay array, the function is supposed to count the total number of birds seen in the specified week. So, if 'week' = 1, for example, the corresponding indexes of 'birdsPerDay' to add together would be 0 - 6, and so on and so forth.

I have tested my code using the provided test cases and in the Chrome console. I tried logging some values to understand where the bug is, but I can't figure out why my counter (named 'totalBirdsInSpecifiedWeek'), which is initialized to '0' is staying at '0' instead of adding the corresponding indexes of 'week' together and return the correct 'totalBirdsInSpecifiedWeek'. I have tried changing the placement of the return statement as well, but that didn't result in any change.

Below is the code I have written:

export function birdsInWeek(birdsPerDay, week) {
  let totalBirdsInSpecifiedWeek = 0
  if (week === 1) {
    for (let i = 0; i < birdsPerDay[7]; i++) {
      totalBirdsInSpecifiedWeek += birdsPerDay[i];
    } 
    return totalBirdsInSpecifiedWeek;
  } else {
  for (let i = week * 7 - 7; i < birdsPerDay[week * 7]; i++) {
    totalBirdsInSpecifiedWeek += birdsPerDay[i];
  }
  return totalBirdsInSpecifiedWeek;
};
}

Where did I go wrong?

  • looks like `i < birdsPerDay[7]` should be `i < 7`. – gog Aug 24 '22 at 15:49
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – VLAZ Aug 24 '22 at 15:50
  • Hey @gog I changed both instances where I was trying to use an index of birdsPerDay to measure against the value of the iterator and it fixed the problem! Thank you so much for that. I don't understand why it wouldn't work the other way, though. – user15377612 Aug 24 '22 at 15:53
  • @VLAZ thank you for the links! I'm still figuring out how to use debugger at the moment and the links seem really useful after skimming the content. (: – user15377612 Aug 24 '22 at 15:58
  • https://pythontutor.com/visualize.html can help too. Despite its name it does also support JS (look at the dropdown). It has a beginner-friendly visualization and also allows you to time-travel _backwards_ in your code execution to really understand what's going on. – CherryDT Aug 24 '22 at 15:59
  • Both of your for loops need to count up to 7 (or some multiple of 7) because that's how many days are in a week. There are not `birdsPerDay[7]` days in a week. – James Aug 24 '22 at 16:02

3 Answers3

0

birdsPerDay[7] this means values of birdsPerDay array at index 7 of the array so the condition would loop checking if i is lesser than that value sometimes it would even hit the NaN so the idea is to just check on either length or index of the array to get an accurate response ..

0

Per the commenter @gog, I changed the 4th and 9th lines of code to correct the stopping condition in each of the for loops so they are written as indexes of the 'birdsPerDay' array and instead just numerical values.

export function birdsInWeek(birdsPerDay, week) {
  let totalBirdsInSpecifiedWeek = 0
  if (week === 1) {
    for (let i = 0; i < 7; i++) {
      totalBirdsInSpecifiedWeek += birdsPerDay[i];
    } 
    return totalBirdsInSpecifiedWeek;
  } else {
  for (let i = week * 7 - 7; i < week * 7; i++) {
    totalBirdsInSpecifiedWeek += birdsPerDay[i];
  }
  return totalBirdsInSpecifiedWeek;
}; return totalBirdsInSpecifiedWeek;
}
0

I found the exercise, according to the problem posed, I think this is the solution. bird-watcher - exercism

Explanation, the array is divided by the days of the week, then the array is divided by that amount from 0 to 6 (Monday to Sunday), then I used a nested "for" to iterate over the corresponding week, I used another "for" to do the addition, return the value from the function and return the result.

let countsbirdsPerDay = [2, 5, 0, 7, 4, 1, 3, 0, 2, 5, 0, 1, 3, 1];
let userWeek = 2;

const birdsInWeek = (birdsPerDay, week) => {
    let segmentWeek = Math.round(birdsPerDay.length / week);
    let total = 0;
    for (let i = 0; i < week; i++) {
        views = birdsPerDay.slice(i * segmentWeek, segmentWeek * (i + 1));
        if ((i + 1) == week) {
            for (let x = 0; x < views.length; x++) {
                total = total + views[x];
            }       
        }
    }
    return total;
}

console.log(birdsInWeek(countsbirdsPerDay, userWeek));
TomásVF
  • 13
  • 6