Hi,
i started doing the Javascript track and I think I encountered a bad test in the Bird Watcher exercise:
Test 9 says:
const birdsPerDay = [2, 0, 1, 4, 1, 3, 0];
expect(Object.is(fixBirdCountLog(birdsPerDay), birdsPerDay)).toBe(true);
which I believe it will always fail.
According to MDN documentation, Object.is(obj1, obj2)
returns true if obj1
and obj2
are the same object, that is, both values reference the same object in memory;
In fact, one can check
let arr = [];
console.log(Object.is(arr,arr)); // true
console.log(Object.is([],arr)); // false
console.log(Object.is([],[])); // false
let newArr = ['a'];
console.log(Object.is(newArr ,newArr )); // true
console.log(Object.is(['a'],newArr )); // false
console.log(Object.is(['a'],['a'])); // false
Considering that in the test birdsPerDay
is a constant (immutable object), the test will fail if one creates a function that
- modifies the input array
- creates a new array from the input
Test 9 checks that the output is an array (which I think is redundant considering what Test 10 and Test 11 do) so I would suggest to use the method Array.isArray(value)
const birdsPerDay = [2, 0, 1, 4, 1, 3, 0];
expect(Array.isArray(fixBirdCountLog(birdsPerDay))).toBe(true);
I have tried the following but they all fail Test 9 and pass Test 10 and Test 11:
export function fixBirdCountLog(birdsPerDay) {
// I assume it fails since it can't modify a constant
return birdsPerDay.map((num,ind)=>(ind%2==0)?num+1:num);
}
export function fixBirdCountLog(birdsPerDay) {
let newArr = []; // create a new array to return since the input is a constant
birdsPerDay.forEach((num,ind)=>(ind%2==0) ? newArr.push(num+1):newArr.push(num));
return newArr;
}
export function fixBirdCountLog(birdsPerDay) {
let newArr = []; // create a new array to return since the input is a constant
for(let ind=0; ind<birdsPerDay.length; ind++){
if (ind%2==0) newArr.push(birdsPerDay[ind]+1);
else newArr.push(birdsPerDay[ind]);
}
return newArr;
}