#I have an issue in 8th problem (Bird Watcher) and error showing in task 7. (JavaScript).

43 messages · Page 1 of 1 (latest)

elder valve
#

This is my code:

export function birdsInWeek(birdsPerDay, week) {
let totalBirds = 0;
for (let i = 0; i <= birdsPerDay.length - 1; i++) {
if (week === 1 && i <= 7) {
totalBirds = totalBirds + birdsPerDay[i];
console.log(totalBirds);
} else if (week === 2 && i >= 8 && i <= 14) {
totalBirds = totalBirds + birdsPerDay[i];
} else if (week !== 1 && week !== 2) {
totalBirds = totalBirds + birdsPerDay[i];
}
}
return totalBirds;
}

This is my error:

CODE RUN
const week21 = [2, 0, 1, 4, 1, 3, 0];
const birdsPerDay = randomArray(20 * 7)
.concat(week21)
.concat(randomArray(10 * 7));
expect(birdsInWeek(birdsPerDay, 21)).toBe(11);
TEST FAILURE
Error: expect(received).toBe(expected) // Object.is equality

Expected: 11
Received: 25

#

pls someone help me in this. I required some guidance in this.

void anchor
#
  • Do you understand what the test is calling?
  • Do you understand what the test expects and why?
  • Do you understand what your code is producing and why?
elder valve
#
  1. I have to calculate total birds count for per week.
    2)If it is week one means I have calculate for that week right?
    3)If it is week two means I have to calculate for that the give data contains more days so I have used if condition to calculate for week 1 and week 2.
void anchor
#

And what week is that test asking for?

elder valve
#

for week 21

void anchor
#

Does your code handle week 21?

#

Can you write code which can handle any week?

elder valve
#

okay I will try to do

void anchor
#

Do you understand what is needed?

elder valve
#

In every week, I have to find week 1 or week 2 like that only and the give input will be in number but for last task the given week is in array.

#

like this const week21 = [2, 0, 1, 4, 1, 3, 0];
const birdsPerDay = randomArray(20 * 7)
.concat(week21)
.concat(randomArray(10 * 7));
expect(birdsInWeek(birdsPerDay, 21)).toBe(11);

void anchor
#

The input is always an array

#

Given an array of data (one value per day), you need to write a function that can find the total for any week

#

Does that make sense?

elder valve
#

okay

#

for example : this is the input (const birdsPerDay = [4, 7, 3, 2, 1, 1, 2, 0, 2, 3, 2, 7, 1, 3, 0, 6, 5, 3, 7, 2, 3];) and for week = 2;

for this I have to calculate 0th index to 13th index right?

void anchor
#

How many days are in week 2?

elder valve
#

14 days

void anchor
#

Nope

#

A week has 7 days.

elder valve
#

yeah yes yes

void anchor
#

So which indexes do you need to check for week 2?

elder valve
#

for week 2 index of 7 to index of 13 have to check

void anchor
#

And for week 3?

elder valve
#

14 to 20 for week 3

#

like that for week 21 I have to find the index?

void anchor
#

And for any given week, week n ?

#

Could you figure out the index for any given week, n?

elder valve
#

give me a min

void anchor
#

Look for the pattern.
Week 1: 0 to 6.
Week 2: 7 to 13.
Week 3: 14 to 20

elder valve
#

okay

void anchor
#

Can you simplify that to solve for any week, n?

elder valve
#

yes I will try to solve

void anchor
#

Good luck!

elder valve
#

let days = 7;
export function birdsInWeek(birdsPerDay, week) {
let totalBirds = 0;
let totalDays = days * week;
let numWeek = (week - 1) * 7;
console.log(numWeek);
console.log(totalDays);
for (let i = numWeek; i <= totalDays - 1; i++) {
totalBirds = totalBirds + birdsPerDay[i];
}
return totalBirds;
}

#

now I completed

void anchor
#

Congrats!

#

Now you can submit it for a code review and work to improve it even further!

elder valve
#

yeah sure

#

thank you so much

void anchor
#

You're very welcome

elder valve
#

Hello Isaac