Logic review of the number of visiting birds in a specific week

Good day, here’s my code:

 let totalBirds = 0;
  let start = (week - 1) * 7;
  let end = start + 7;

  // Slice out the correct week
  let weekDays = birdsPerDay.slice(start, end);
  
  for (let i = 0; i < weekDays.length; i++) {
    totalBirds += weekDays[i];
  }

It keeps spitting birdsPerDay.slice is not a function

The full code:

function birdsInWeek(birdsPerDay, week) {
  let totalBirds = 0;
  let start = (week - 1) * 7;
  let end = start + 7;

  // Slice out the correct week
  let weekDays = birdsPerDay.slice(start, end);
  
  for (let i = 0; i < weekDays.length; i++) {
    totalBirds += weekDays[i];
  }
  return totalBirds;
}

That’s because there’s no slice function/method on the birdsPerDay object :slight_smile: birdsPerDay is array-like but not actually an array. You can index into it but not use methods like slice().

Hey Isaac, thank you for your input.
I’m following the instruction that that method ought to work on the birdsPerDay, since it will receive an array as per the instruction.

I will be following on what you have said, and I will update this thread.

The instructions are a bit misleading. The function receives an array-like object which you can use to access specific values by index. But you cannot use array methods on it.

Exactly.
I have solved it and now I think it might have been done deliberately to allow folks grind at the problem, for problem solving sake, just my thoughts.
If it ought to be very specific as you have said, instead of “Implement a function totalBirdCount that accepts an array that contains the bird count per day.” Do you think I should open an issue and P.R on it?

You shouldn’t open an issue. Exercism uses the forum to track issues. You shouldn’t open a PR without first proposing a change on the forum and having a track maintainer requesting you PR the change.

1 Like

Alright, thanks.
I’ll propose a change now, I suppose the right section to do so is in the Exercism, right?

Actually, this section (programming> JavaScript) would be best.

2 Likes

It has not been done to “Grind the problem”. It has been done because the concept being taught is for loops.

If you feel that the following text would not have caused you to keep trying to slice the array, we’re happy to receive a PR for it:

that accepts a list that contains the bird count per day.
3 Likes

Alright, thanks.