#Help I'm stuck with this exercise bird watcher

1 messages ยท Page 1 of 1 (latest)

dense crown
#
export function birdsInWeek(birdsPerDay, week) {
  let total = 0;
  for(let i = 0; i < birdsPerDay.length; i++) {
    total += birdsPerDay[i];
  }
  return total;
}

I have read the exercise many times, but I cannot understand it well. What makes it difficult for me to start.

2 task: Now that you got a general feel for your bird count numbers, you want to make a more fine-grained analysis.

Implement a function birdsInWeek that accepts an array of bird counts per day and a week number. It returns the total number of birds that you counted in that specific week. You can assume weeks are always tracked completely.

mortal stag
#

The array birdsPerDay contains counts, one per day, that represent how many birds have been spotted on that particular day.
For example: birdsPerDay = [2, 5, 0, 7, 4, 1, 3, 0, 2, 5, 0, 1, 3, 1]; means that in the first week you've seen 2, 5, 0, 7, 4, 1, and 3 birds (seven days per week), 22 in total. In the second week you've seen 0, 2, 5, 0, 1, 3, and 1 birds, 12 in total.

For task #2 you're supposed to implement the function birdsInWeek that gets as parameters the array birdsPerDay and the number week. You have to calculate how many birds you've seen in that specific week.

#

Do you understand the example?

birdsPerDay = [2, 5, 0, 7, 4, 1, 3, 0, 2, 5, 0, 1, 3, 1];
birdsInWeek(birdsPerDay, 2);
// => 12
dense crown
#

I understand but I am confused by the fact that I have to take into account the index of the beginning and end.

mortal stag
#

How would you as a human perform this task if I gave you that array above and asked you about the number of birds in week 2?

dense crown
#

I don't know, it depends on how many weeks it is, if it's week 2, then week 1 shouldn't count.

lethal cedar
#

What @mortal stag asking you is if you have a physical calendar of a month start from day 1, each day has a number of birds on it. How would you count the sum when he asked you to start counting only bird for the 2nd week.

dense crown
#

I would count from 7

lethal cedar
#

You are on the right track. What if he ask for 3rd week?

dense crown
#

14

lethal cedar
#

Right, so you recognize that there is a pattern for the starting day, yeah? Depend on the asking week

dense crown
#

Yes

lethal cedar
#

So now you can apply that logic, you find the start counting day (the index) , and then you count for a week.

dense crown
#

I'm thinking how to apply it

lethal cedar
#

Think of how you usually start a loop from a -> b. Like counting from 0 to 10, start from 0 and increment 1 by 1 to 10. But this time, the starting point is not a fixed 0, it is a dynamic value based on the input of the week in focus

dense crown
#

I understand, but if it starts with a dynamic variable, that is, with a week, and it has to count from 7, I don't understand how to do it.

#

I have to make it start at the number that ends the week, that is, like a range

lethal cedar
#

Week 1: ?
Week 2: 7
Week 3: 14
Week 4: ??
Week 5: ??

If u can fill this out and see the pattern, then you can get the starting day value.
Hint: it related to how many days are in a week

dense crown
#

Week 1: 1
Week 2: 7
Week 3: 14
Week 4: 21
Week 5: 1

lethal cedar
#

oh yeah my bad i should clarify that in this case we dont use normal calendar anymore, it is just a long year of weeks. so week 5 shouldnt wrap around and be 28 in your case

#

if week 1 start at day 1, would week 2 start at day 7?

lethal cedar
#

If monday was start day aka day 1, what day is next monday?

dense crown
#

But I can't think that starting at 7 would be 1 and then do the counting again.

timid spindle
#

What would the last day be for weeks 1, 2, 3?

dense crown
#

My head doesn't work hahaha

timid spindle
#

7 for week 1? What about week 2? And week 3?

dense crown
#

I'm thinking as if it were a range, 1 week is 7, but if it's 3 weeks it would be 21

timid spindle
#

What is the start index and end index for week 1, week 2 and week 3?

#
Week 1:  <start> - <end>
Week 2:  <start> - <end>
Week 3:  <start> - <end>
dense crown
#
Week 1:  1 - 7
Week 2:  8 - 15
Week 3:  16 - 23
timid spindle
#

Is day 7 part of week 1 or week 2?

#

Is day 15 part of week 2 or week 3 ๐Ÿ˜„

#

Week 1 is 7 - 1 = 6 days long. Week 2 is 15 - 8 = 7 days long. Week 3 is 23 - 16 = 7 days long.

#

Well. I did math wrong, since those ranges are inclusive.

dense crown
#

Why 6?

timid spindle
#

Week 1 is 1..7 = 7 days long. Week 2 is 8..15 = 8 days long. Week 3 is 16..23 = 8 days long.

#

The end - start should be the same for all weeks.
Week 1: 7 - 1 = 6. Week 2: 15 - 8 = 7. Week 3: 23 - 16 = 7.

#

That suggests your start/end numbers might be wrong:

Week 1:  1 - 7
Week 2:  8 - 15
Week 3:  16 - 23
#

Does that seem right? Do you see the issue?

dense crown
#

I do not see the problem

timid spindle
#

Week 1 is a different length than week 2 and week 3

#

How many days are in 3 weeks?

dense crown
timid spindle
#

Week 1 to week 3 is days 1-23?

timid spindle
#

Try writing out numbers like a calendar

dense crown
timid spindle
#

How many days are in 3 weeks? 23 days?

dense crown
timid spindle
#

How many days are in one week?

dense crown
timid spindle
#

How many days in two weeks?

dense crown
#

14

timid spindle
#

How many days in three weeks?

dense crown
#

21?

timid spindle
#

Pull out a calendar and count ๐Ÿ˜‰

#

What days are in week 1? Week 2? Week 3?

Week 1: start - end
Week 2: start - end
Week 3: start - end
dense crown
cyan silo
#

April 2024 would be a better example...?

timid spindle
timid spindle
# dense crown

No 23 then.
What days are in week 1? Week 2? Week 3?

Week 1: start - end
Week 2: start - end
Week 3: start - end
dense crown
#
Week 1:  1 - 7
Week 2:  8 - 14
Week 3:  15 - 21

timid spindle
#

That seems more reasonable. Now they are all the same length ๐Ÿ˜
And you have that nice pattern of 7,14,21 which matches the days in a week!

#

Do you see a pattern in those numbers?

#

What would be the start and end for week 10?

dense crown
dense crown
timid spindle
#

That makes sense, given that a week has 7 days

#

What's the start and end for week x?

#

Note 70-63=7 which is different from weeks 1,2,3

dense crown
timid spindle
#

What about week=8?
Can you figure out out in terms of a variable, n?

dense crown
#

I can't take my stress anymore

timid spindle
#

Take a break! Try again later.

dense crown
#

Can you explain it to me as if I were 5 years old?

timid spindle
#

If week 1 ends on day 7, and
If week 2 ends on day 14, and
If week 3 ends on day 21, then

When does week x end?

cyan silo
#

or which multiplication table represents this sequence, maybe?

dense crown
#

X, what would it be?

#

1 x7
2 x7
3 x7
4x7

cyan silo
#

you are almost there, can you answer my last question? The pattern you need is in the answer...

timid spindle
#

Exactly!

#

And for week 10?

dense crown
#

10 x7

timid spindle
#

And for week number?

#

Where number is a variable...

dense crown
#

week x 7?

timid spindle
#

Yes

#

Or number * 7 technically

dense crown
#

Okay

timid spindle
#

That gives you the end of any week. Can you do something similar to find the start of any week?

dense crown
#

Yes that's fine

timid spindle
#

If you can find a formula for the start and end of any week, you can use that to get the range of days in any week

#

Which can be used to count the birds in any week

dense crown
#
let indexEnd = week * 7;
let indexStart = week - 1 * 7;
timid spindle
#

That looks roughly correct

dense crown
timid spindle
#

๐ŸŽ‰

austere spearBOT
#

If everything is resolved, we ask that the person who posted the request react to the top/original post with a :white_check_mark: (:white_check_mark:). This indicates to others that this issue has been resolved and locks the thread.
If all the tests pass and you want to further improve your solution, we encourage you to use the "Request a Code Review" feature on the website!

dense crown