Bird Watcher - Task 3 Help Please

The test tells me a new array is not create. However, I am creating a new array:

" FAILED

Test 9

fixBirdCountLog > does not create a new array

|0x0

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"

My code:

  • 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) {
    const correctedBirdCount = [];
    for (let i = 0; i < birdsPerDay.length; i += 2) {
    correctedBirdCount.push(birdsPerDay[i] + 1);
    if (i + 1 < birdsPerDay.length) {
    correctedBirdCount.push(birdsPerDay[i + 1]);
    }
    }

Am I misunderstanding the issue? All the other tests in Task 3 passed.

In this exercise the function should modify its parameter and return it.

But this function builds a brand new array.
Also, I cannot see a return statement.

Actually, it should return a new array.

Which it seems to build, but not return.

@MatthijsBlom Are you sure?
JavaScript is not my strong suit, but I think the test ‘does not create a new array’ explicitly requires the parameter to be returned.

No, you’re right. I misread the test result as a complaint instead of as a requirement.

@nstanovic When confronted with an unclear test failure it often helps to read the code of the test in question.

If you are working locally you should have the bird-watcher.spec.js file, and otherwise you’d have to look it up in the track repo. Sieben has linked to it above.

Do you understand this code? If not, which parts do you not understand?

This test failed meaning it expects that you do not create a new array, but you did.

Hello and thank you very much for the tips and advice. I misunderstood what the test wanted as a passing condition. I am confused at how I will modify the values of a const array. I assumed that const arrays cannot have their values changed. If this is not the case, I will do more research. My code did have a return statement but I forgot to post it as part of the code previously. Tomorrow, my goal will be to figure out a solution. Thanks again very much!

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

return correctedBirdCount;
}

  1. You can use code blocks to post code.

```javascript
code here
```

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

return correctedBirdCount;
}
  1. Did you look at the test code? Do you understand what it is testing? Do you understand why your code fails that test requirement?
  test('does not create a new array', () => {
    const birdsPerDay = [2, 0, 1, 4, 1, 3, 0];

    // This checks that the same object that was passed in is returned.
    // https://jestjs.io/docs/expect#tobevalue
    expect(Object.is(fixBirdCountLog(birdsPerDay), birdsPerDay)).toBe(true);
  });

I ended up fixing it and getting it right:

export function fixBirdCountLog(birdsPerDay) {
  for (let i = 0; i < birdsPerDay.length; i += 2) {
    birdsPerDay[i] = birdsPerDay[i]+1
  }
  console.log(birdsPerDay)
  return birdsPerDay;
}

The const prevents the re-assignment of the initial name you assigned to the array, but it does not prevent changing the contents of the array. You’ll get an error only if you write const x = [1]; x = [2] but not when you write const x = [1]; x[0] = 2