Skip to content

Commit bcdaf32

Browse files
gagan-bhullar-technaomi-lgbtlasjorg
authored
fix(curriculum): sort wrong validation (freeCodeCamp#55896)
Co-authored-by: Naomi the Technomancer <[email protected]> Co-authored-by: Lasse Jørgensen <[email protected]>
1 parent 80700c4 commit bcdaf32

36 files changed

+375
-233
lines changed

curriculum/challenges/_meta/learn-advanced-array-methods-by-building-a-statistics-calculator/meta.json

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -109,136 +109,140 @@
109109
"title": "Step 25"
110110
},
111111
{
112-
"id": "635090f47eb6d9563a6fed05",
112+
"id": "66c395a16c3ff33dc6f8e7c0",
113113
"title": "Step 26"
114114
},
115115
{
116-
"id": "6352e79d15aae30fac58f48e",
116+
"id": "635090f47eb6d9563a6fed05",
117117
"title": "Step 27"
118118
},
119119
{
120-
"id": "6352e80e024e89111600edfb",
120+
"id": "6352e79d15aae30fac58f48e",
121121
"title": "Step 28"
122122
},
123123
{
124-
"id": "6352e93db104661305c5f658",
124+
"id": "6352e80e024e89111600edfb",
125125
"title": "Step 29"
126126
},
127127
{
128-
"id": "6352e96d2604f813c656750b",
128+
"id": "6352e93db104661305c5f658",
129129
"title": "Step 30"
130130
},
131131
{
132-
"id": "65aac0678d51b3f1a0cb8061",
132+
"id": "6352e96d2604f813c656750b",
133133
"title": "Step 31"
134134
},
135135
{
136-
"id": "65c4dc57418fd6bfc710d61d",
136+
"id": "65aac0678d51b3f1a0cb8061",
137137
"title": "Step 32"
138138
},
139139
{
140-
"id": "65ca2d0625aa3a3201067f70",
140+
"id": "65c4dc57418fd6bfc710d61d",
141141
"title": "Step 33"
142142
},
143143
{
144-
"id": "65e62efde0592ec4b4bb6a69",
144+
"id": "65ca2d0625aa3a3201067f70",
145145
"title": "Step 34"
146146
},
147147
{
148-
"id": "65f83a7ca7047318e3ccff7c",
148+
"id": "65e62efde0592ec4b4bb6a69",
149149
"title": "Step 35"
150150
},
151151
{
152-
"id": "6352ea3a5b79e614ee2282fd",
152+
"id": "65f83a7ca7047318e3ccff7c",
153153
"title": "Step 36"
154154
},
155155
{
156-
"id": "6352ebd3ab962c168a122e85",
156+
"id": "6352ea3a5b79e614ee2282fd",
157157
"title": "Step 37"
158158
},
159159
{
160-
"id": "6352ec8b9c70fd17b8c7ba3f",
160+
"id": "6352ebd3ab962c168a122e85",
161161
"title": "Step 38"
162162
},
163163
{
164-
"id": "6352ecef9f045519063da9b3",
164+
"id": "6352ec8b9c70fd17b8c7ba3f",
165165
"title": "Step 39"
166166
},
167167
{
168-
"id": "6352edee8a4de01ad693f0e4",
168+
"id": "6352ecef9f045519063da9b3",
169169
"title": "Step 40"
170170
},
171171
{
172-
"id": "6352ee566a59d31d24bde74b",
172+
"id": "6352edee8a4de01ad693f0e4",
173173
"title": "Step 41"
174174
},
175175
{
176-
"id": "6352f09b1e53a420e7873344",
176+
"id": "6352ee566a59d31d24bde74b",
177177
"title": "Step 42"
178178
},
179179
{
180-
"id": "6352f179bdca23221298a5ba",
180+
"id": "6352f09b1e53a420e7873344",
181181
"title": "Step 43"
182182
},
183183
{
184-
"id": "6352f2526dccb523150b64fb",
184+
"id": "6352f179bdca23221298a5ba",
185185
"title": "Step 44"
186186
},
187187
{
188-
"id": "6352f2a24eb71b24284ca2b6",
188+
"id": "6352f2526dccb523150b64fb",
189189
"title": "Step 45"
190190
},
191191
{
192-
"id": "6352faf71a9db52631864634",
192+
"id": "6352f2a24eb71b24284ca2b6",
193193
"title": "Step 46"
194194
},
195195
{
196-
"id": "6352fbb93a91a8272f838d42",
196+
"id": "6352faf71a9db52631864634",
197197
"title": "Step 47"
198198
},
199199
{
200-
"id": "6352fcb156834128001ea945",
200+
"id": "6352fbb93a91a8272f838d42",
201201
"title": "Step 48"
202202
},
203203
{
204-
"id": "6352fce75b2d3b2924930f1e",
204+
"id": "6352fcb156834128001ea945",
205205
"title": "Step 49"
206206
},
207207
{
208-
"id": "6352fe473d53592a40ae403b",
208+
"id": "6352fce75b2d3b2924930f1e",
209209
"title": "Step 50"
210210
},
211211
{
212-
"id": "6352fed209792d2b89e92ea1",
212+
"id": "6352fe473d53592a40ae403b",
213213
"title": "Step 51"
214214
},
215215
{
216-
"id": "6352ff27e0e51b2c7dce0010",
216+
"id": "6352fed209792d2b89e92ea1",
217217
"title": "Step 52"
218218
},
219219
{
220-
"id": "6352ffe4cfafa72d595a0007",
220+
"id": "6352ff27e0e51b2c7dce0010",
221221
"title": "Step 53"
222222
},
223223
{
224-
"id": "6353004b235d7a2e0b913f2b",
224+
"id": "6352ffe4cfafa72d595a0007",
225225
"title": "Step 54"
226226
},
227227
{
228-
"id": "6353024f5eab012fa2f57eec",
228+
"id": "6353004b235d7a2e0b913f2b",
229229
"title": "Step 55"
230230
},
231231
{
232-
"id": "6353028147d3c7309017216a",
232+
"id": "6353024f5eab012fa2f57eec",
233233
"title": "Step 56"
234234
},
235+
{
236+
"id": "6353028147d3c7309017216a",
237+
"title": "Step 57 "
238+
},
235239
{
236240
"id": "635302be760d6031d11a06cd",
237-
"title": "Step 57"
241+
"title": "Step 58"
238242
},
239243
{
240244
"id": "6374249d3fbf2a5b079ba036",
241-
"title": "Step 58"
245+
"title": "Step 59"
242246
}
243247
]
244-
}
248+
}

curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-advanced-array-methods-by-building-a-statistics-calculator/63508c898d753754757bd5e3.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ dashedName: step-21
77

88
# --description--
99

10-
By default, the `.sort()` method converts the elements of an array into strings, then sorts them alphabetically. This works well for strings, but not so well for numbers. For example, `10` comes before `2` when sorted as strings, but `2` comes before `10` when sorted as numbers.
10+
By default, the `.sort()` method converts the elements of an array into strings, then sorts them alphabetically. The `.sort()` method mutates the original array. This works well for strings, but not so well for numbers. For example, `10` comes before `2` when sorted as strings, but `2` comes before `10` when sorted as numbers.
1111

1212
To fix this, you can pass in a callback function to the `.sort()` method. This function takes two arguments, which represent the two elements being compared. The function should return a value less than `0` if the first element should come before the second element, a value greater than `0` if the first element should come after the second element, and `0` if the two elements should remain in their current positions.
1313

curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-advanced-array-methods-by-building-a-statistics-calculator/635090f47eb6d9563a6fed05.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
---
22
id: 635090f47eb6d9563a6fed05
3-
title: Step 26
3+
title: Step 27
44
challengeType: 0
5-
dashedName: step-26
5+
dashedName: step-27
66
---
77

88
# --description--
99

1010
Now it is time to apply what you have learned to the `getMedian` function.
1111

12-
Inside your `getMedian` function, check if the length of `array` is even. If it is, find the middle two numbers, calculate their mean, and return the result. If the length of `array` is odd, return the middle number.
12+
Inside your `getMedian` function, check if the length of `sorted` is even. If it is, find the middle two numbers, calculate their mean, and return the result. If the length of `sorted` is odd, return the middle number.
1313

1414
Make sure to work with the `sorted` array to find the middle numbers.
1515

1616
Also if you need help, refer back to the previous few steps to see how to find the median for an array.
1717

1818
# --hints--
1919

20+
Your `getMedian` function should use `sorted` array.
21+
22+
```js
23+
assert.equal(getMedian.toString().match(/array/g).length, 2);
24+
```
2025

2126
Your `getMedian` function should return the mean of the middle two numbers if the array length is even.
2227

@@ -123,7 +128,7 @@ const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length
123128

124129
--fcc-editable-region--
125130
const getMedian = (array) => {
126-
const sorted = array.sort((a, b) => a - b);
131+
const sorted = array.toSorted((a, b) => a - b);
127132
}
128133
--fcc-editable-region--
129134

curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-advanced-array-methods-by-building-a-statistics-calculator/6352e79d15aae30fac58f48e.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
id: 6352e79d15aae30fac58f48e
3-
title: Step 27
3+
title: Step 28
44
challengeType: 0
5-
dashedName: step-27
5+
dashedName: step-28
66
---
77

88
# --description--
@@ -121,11 +121,11 @@ input {
121121
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
122122

123123
const getMedian = (array) => {
124-
const sorted = array.sort((a, b) => a - b);
124+
const sorted = array.toSorted((a, b) => a - b);
125125
const median =
126-
array.length % 2 === 0
127-
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
128-
: sorted[Math.floor(array.length / 2)];
126+
sorted.length % 2 === 0
127+
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
128+
: sorted[Math.floor(sorted.length / 2)];
129129
return median;
130130
}
131131

curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-advanced-array-methods-by-building-a-statistics-calculator/6352e80e024e89111600edfb.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
id: 6352e80e024e89111600edfb
3-
title: Step 28
3+
title: Step 29
44
challengeType: 0
5-
dashedName: step-28
5+
dashedName: step-29
66
---
77

88
# --description--
@@ -119,11 +119,11 @@ input {
119119
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
120120

121121
const getMedian = (array) => {
122-
const sorted = array.sort((a, b) => a - b);
122+
const sorted = array.toSorted((a, b) => a - b);
123123
const median =
124-
array.length % 2 === 0
125-
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
126-
: sorted[Math.floor(array.length / 2)];
124+
sorted.length % 2 === 0
125+
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
126+
: sorted[Math.floor(sorted.length / 2)];
127127
return median;
128128
}
129129

curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-advanced-array-methods-by-building-a-statistics-calculator/6352e93db104661305c5f658.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
id: 6352e93db104661305c5f658
3-
title: Step 29
3+
title: Step 30
44
challengeType: 0
5-
dashedName: step-29
5+
dashedName: step-30
66
---
77

88
# --description--
@@ -136,11 +136,11 @@ input {
136136
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
137137

138138
const getMedian = (array) => {
139-
const sorted = array.sort((a, b) => a - b);
139+
const sorted = array.toSorted((a, b) => a - b);
140140
const median =
141-
array.length % 2 === 0
142-
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
143-
: sorted[Math.floor(array.length / 2)];
141+
sorted.length % 2 === 0
142+
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
143+
: sorted[Math.floor(sorted.length / 2)];
144144
return median;
145145
}
146146

curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-advanced-array-methods-by-building-a-statistics-calculator/6352e96d2604f813c656750b.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
id: 6352e96d2604f813c656750b
3-
title: Step 30
3+
title: Step 31
44
challengeType: 0
5-
dashedName: step-30
5+
dashedName: step-31
66
---
77

88
# --description--
@@ -108,14 +108,14 @@ input {
108108
```js
109109
const getMean = array => array.reduce((acc, el) => acc + el, 0) / array.length;
110110

111-
const getMedian = array => {
112-
const sorted = array.sort((a, b) => a - b);
111+
const getMedian = (array) => {
112+
const sorted = array.toSorted((a, b) => a - b);
113113
const median =
114-
array.length % 2 === 0
115-
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
116-
: sorted[Math.floor(array.length / 2)];
114+
sorted.length % 2 === 0
115+
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
116+
: sorted[Math.floor(sorted.length / 2)];
117117
return median;
118-
};
118+
}
119119

120120
const getMode = array => {
121121
const counts = {};

curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-advanced-array-methods-by-building-a-statistics-calculator/6352ea3a5b79e614ee2282fd.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
id: 6352ea3a5b79e614ee2282fd
3-
title: Step 36
3+
title: Step 37
44
challengeType: 0
5-
dashedName: step-36
5+
dashedName: step-37
66
---
77

88
# --description--
@@ -130,11 +130,11 @@ input {
130130
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
131131

132132
const getMedian = (array) => {
133-
const sorted = array.sort((a, b) => a - b);
133+
const sorted = array.toSorted((a, b) => a - b);
134134
const median =
135-
array.length % 2 === 0
136-
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
137-
: sorted[Math.floor(array.length / 2)];
135+
sorted.length % 2 === 0
136+
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
137+
: sorted[Math.floor(sorted.length / 2)];
138138
return median;
139139
}
140140

curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-advanced-array-methods-by-building-a-statistics-calculator/6352ebd3ab962c168a122e85.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
id: 6352ebd3ab962c168a122e85
3-
title: Step 37
3+
title: Step 38
44
challengeType: 0
5-
dashedName: step-37
5+
dashedName: step-38
66
---
77

88
# --description--
@@ -121,11 +121,11 @@ input {
121121
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
122122

123123
const getMedian = (array) => {
124-
const sorted = array.sort((a, b) => a - b);
124+
const sorted = array.toSorted((a, b) => a - b);
125125
const median =
126-
array.length % 2 === 0
127-
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
128-
: sorted[Math.floor(array.length / 2)];
126+
sorted.length % 2 === 0
127+
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
128+
: sorted[Math.floor(sorted.length / 2)];
129129
return median;
130130
}
131131

0 commit comments

Comments
 (0)