Skip to content

Commit 18e8682

Browse files
Bird Watcher: Prevent not following the instructions (#2667)
* Prevent not following the instructions * Update stub
1 parent 60a4856 commit 18e8682

File tree

4 files changed

+86
-33
lines changed

4 files changed

+86
-33
lines changed

exercises/concept/bird-watcher/.docs/instructions.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ You already digitalized the bird counts per day for the past weeks that you kept
66

77
Now you want to determine the total number of birds that you counted, calculate the bird count for a specific week and correct a counting mistake.
88

9-
<!-- prettier-ignore-start -->
9+
<!-- prettier-ignore -->
1010
~~~~exercism/note
11-
To practice, use a for loop to solve each of the tasks below.
11+
To practice, use a `for` loop to solve each of the tasks below.
1212
~~~~
13-
<!-- prettier-ignore-end -->
1413

1514
## 1. Determine the total number of birds that you counted so far
1615

exercises/concept/bird-watcher/.meta/exemplar.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
*/
1313
export function totalBirdCount(birdsPerDay) {
1414
let total = 0;
15+
1516
for (let i = 0; i < birdsPerDay.length; i++) {
1617
total += birdsPerDay[i];
1718
}
19+
1820
return total;
1921
}
2022

@@ -27,10 +29,12 @@ export function totalBirdCount(birdsPerDay) {
2729
*/
2830
export function birdsInWeek(birdsPerDay, week) {
2931
let total = 0;
32+
3033
const start = 7 * (week - 1);
3134
for (let i = start; i < start + 7; i++) {
3235
total += birdsPerDay[i];
3336
}
37+
3438
return total;
3539
}
3640

@@ -39,11 +43,10 @@ export function birdsInWeek(birdsPerDay, week) {
3943
* by one for every second day.
4044
*
4145
* @param {number[]} birdsPerDay
42-
* @returns {number[]} corrected bird count data
46+
* @returns {void} should not return anything
4347
*/
4448
export function fixBirdCountLog(birdsPerDay) {
4549
for (let i = 0; i < birdsPerDay.length; i += 2) {
4650
birdsPerDay[i]++;
4751
}
48-
return birdsPerDay;
4952
}

exercises/concept/bird-watcher/bird-watcher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function birdsInWeek(birdsPerDay, week) {
3030
* by one for every second day.
3131
*
3232
* @param {number[]} birdsPerDay
33-
* @returns {number[]} corrected bird count data
33+
* @returns {void} should not return anything
3434
*/
3535
export function fixBirdCountLog(birdsPerDay) {
3636
throw new Error('Please implement the fixBirdCountLog function');

exercises/concept/bird-watcher/bird-watcher.spec.js

Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,130 @@
11
import { describe, expect, test } from '@jest/globals';
22
import { birdsInWeek, fixBirdCountLog, totalBirdCount } from './bird-watcher';
33

4+
const customInspectSymbol = Symbol.for('nodejs.util.inspect.custom');
5+
const customLogSymbol = Symbol.for('exercism.javascript.util.log');
6+
7+
// Follow the instructions in case you are stuck on "list.method is not a function"
8+
class CountingReport {
9+
constructor(counts) {
10+
// Enables array[index]
11+
counts.forEach((count, index) => {
12+
this[index] = count;
13+
});
14+
15+
// Enables .length
16+
this.length = counts.length;
17+
}
18+
19+
// Log value in non-upgraded environments
20+
toString() {
21+
return arrayOf(this).toString();
22+
}
23+
24+
// Overrides logging in node (ie. students working locally)
25+
[customInspectSymbol]() {
26+
return `Seen birds per day: ${arrayOf(this)}`;
27+
}
28+
29+
// Overrides log overrides in web environment (ie. students working in editor)
30+
[customLogSymbol]() {
31+
return `Seen birds per day: ${arrayOf(this)}`;
32+
}
33+
}
34+
35+
function report(...values) {
36+
return new CountingReport(values);
37+
}
38+
39+
function arrayOf(countingReport) {
40+
return Array.from(
41+
{ length: countingReport.length },
42+
(_, i) => countingReport[i],
43+
);
44+
}
45+
46+
function randomArray(length) {
47+
return Array.from({ length }, () => Math.floor(Math.random() * 8));
48+
}
49+
450
describe('totalBirdCount', () => {
551
test('calculates the correct total number of birds', () => {
6-
const birdsPerDay = [9, 0, 8, 4, 5, 1, 3];
52+
const birdsPerDay = report(9, 0, 8, 4, 5, 1, 3);
753
expect(totalBirdCount(birdsPerDay)).toBe(30);
854
});
955

1056
test('works for a short bird count list', () => {
11-
const birdsPerDay = [2];
57+
const birdsPerDay = report(2);
1258
expect(totalBirdCount(birdsPerDay)).toBe(2);
1359
});
1460

1561
test('works for a long bird count list', () => {
1662
// prettier-ignore
17-
const birdsPerDay = [2, 8, 4, 1, 3, 5, 0, 4, 1, 6, 0, 3, 0, 1, 5, 4, 1, 1, 2, 6];
63+
const birdsPerDay = report(
64+
2, 8, 4, 1, 3, 5, 0, 4, 1, 6, 0, 3, 0, 1, 5, 4, 1, 1, 2, 6
65+
);
66+
1867
expect(totalBirdCount(birdsPerDay)).toBe(57);
1968
});
2069
});
2170

2271
describe('birdsInWeek', () => {
2372
test('calculates the number of birds in the first week', () => {
24-
const birdsPerDay = [3, 0, 5, 1, 0, 4, 1, 0, 3, 4, 3, 0, 8, 0];
73+
const birdsPerDay = report(3, 0, 5, 1, 0, 4, 1, 0, 3, 4, 3, 0, 8, 0);
2574
expect(birdsInWeek(birdsPerDay, 1)).toBe(14);
2675
});
2776

2877
test('calculates the number of birds for a week in the middle of the log', () => {
2978
// prettier-ignore
30-
const birdsPerDay = [4, 7, 3, 2, 1, 1, 2, 0, 2, 3, 2, 7, 1, 3, 0, 6, 5, 3, 7, 2, 3];
79+
const birdsPerDay = report(4, 7, 3, 2, 1, 1, 2, 0, 2, 3, 2, 7, 1, 3, 0, 6, 5, 3, 7, 2, 3);
3180
expect(birdsInWeek(birdsPerDay, 2)).toBe(18);
3281
});
3382

3483
test('works when there is only one week', () => {
35-
const birdsPerDay = [3, 0, 3, 3, 2, 1, 0];
84+
const birdsPerDay = report(3, 0, 3, 3, 2, 1, 0);
3685
expect(birdsInWeek(birdsPerDay, 1)).toBe(12);
3786
});
3887

3988
test('works for a long bird count list', () => {
40-
const week21 = [2, 0, 1, 4, 1, 3, 0];
41-
const birdsPerDay = randomArray(20 * 7)
42-
.concat(week21)
43-
.concat(randomArray(10 * 7));
89+
const week21 = report(2, 0, 1, 4, 1, 3, 0);
90+
const birdsPerDay = report(
91+
...randomArray(20 * 7)
92+
.concat(arrayOf(week21))
93+
.concat(randomArray(10 * 7)),
94+
);
4495

4596
expect(birdsInWeek(birdsPerDay, 21)).toBe(11);
4697
});
4798
});
4899

49100
describe('fixBirdCountLog', () => {
50101
test('returns a bird count list with the corrected values', () => {
51-
const birdsPerDay = [3, 0, 5, 1, 0, 4, 1, 0, 3, 4, 3, 0];
102+
const birdsPerDay = report(3, 0, 5, 1, 0, 4, 1, 0, 3, 4, 3, 0);
52103
const expected = [4, 0, 6, 1, 1, 4, 2, 0, 4, 4, 4, 0];
53-
expect(fixBirdCountLog(birdsPerDay)).toEqual(expected);
54-
});
55-
56-
test('does not create a new array', () => {
57-
const birdsPerDay = [2, 0, 1, 4, 1, 3, 0];
104+
fixBirdCountLog(birdsPerDay);
58105

59-
// This checks that the same object that was passed in is returned.
60-
// https://jestjs.io/docs/expect#tobevalue
61-
expect(Object.is(fixBirdCountLog(birdsPerDay), birdsPerDay)).toBe(true);
106+
expect(arrayOf(birdsPerDay)).toEqual(expected);
62107
});
63108

64109
test('works for a short bird count list', () => {
65-
expect(fixBirdCountLog([4, 2])).toEqual([5, 2]);
110+
const birdsPerDay = report(4, 2);
111+
fixBirdCountLog(birdsPerDay);
112+
113+
expect(arrayOf(birdsPerDay)).toEqual([5, 2]);
66114
});
67115

68116
test('works for a long bird count list', () => {
69117
// prettier-ignore
70-
const birdsPerDay = [2, 8, 4, 1, 3, 5, 0, 4, 1, 6, 0, 3, 0, 1, 5, 4, 1, 1, 2, 6];
118+
const birdsPerDay = report(
119+
2, 8, 4, 1, 3, 5, 0, 4, 1, 6, 0, 3, 0, 1, 5, 4, 1, 1, 2, 6
120+
);
121+
71122
// prettier-ignore
72-
const expected = [3, 8, 5, 1, 4, 5, 1, 4, 2, 6, 1, 3, 1, 1, 6, 4, 2, 1, 3, 6];
73-
expect(fixBirdCountLog(birdsPerDay)).toEqual(expected);
123+
const expected = [
124+
3, 8, 5, 1, 4, 5, 1, 4, 2, 6, 1, 3, 1, 1, 6, 4, 2, 1, 3, 6
125+
]
126+
127+
fixBirdCountLog(birdsPerDay);
128+
expect(arrayOf(birdsPerDay)).toEqual(expected);
74129
});
75130
});
76-
77-
function randomArray(length) {
78-
return Array.from({ length: length }, () => Math.floor(Math.random() * 8));
79-
}

0 commit comments

Comments
 (0)