fix(curriculum): sort wrong validation (#55896)

Co-authored-by: Naomi the Technomancer <accounts+github@nhcarrigan.com>
Co-authored-by: Lasse Jørgensen <28780271+lasjorg@users.noreply.github.com>
This commit is contained in:
Gagan Bhullar
2024-08-20 18:50:49 +02:00
committed by GitHub
co-authored by Naomi the Technomancer Lasse Jørgensen
parent 80700c4f95
commit bcdaf321c2
36 changed files with 375 additions and 233 deletions
@@ -109,136 +109,140 @@
"title": "Step 25"
},
{
"id": "635090f47eb6d9563a6fed05",
"id": "66c395a16c3ff33dc6f8e7c0",
"title": "Step 26"
},
{
"id": "6352e79d15aae30fac58f48e",
"id": "635090f47eb6d9563a6fed05",
"title": "Step 27"
},
{
"id": "6352e80e024e89111600edfb",
"id": "6352e79d15aae30fac58f48e",
"title": "Step 28"
},
{
"id": "6352e93db104661305c5f658",
"id": "6352e80e024e89111600edfb",
"title": "Step 29"
},
{
"id": "6352e96d2604f813c656750b",
"id": "6352e93db104661305c5f658",
"title": "Step 30"
},
{
"id": "65aac0678d51b3f1a0cb8061",
"id": "6352e96d2604f813c656750b",
"title": "Step 31"
},
{
"id": "65c4dc57418fd6bfc710d61d",
"id": "65aac0678d51b3f1a0cb8061",
"title": "Step 32"
},
{
"id": "65ca2d0625aa3a3201067f70",
"id": "65c4dc57418fd6bfc710d61d",
"title": "Step 33"
},
{
"id": "65e62efde0592ec4b4bb6a69",
"id": "65ca2d0625aa3a3201067f70",
"title": "Step 34"
},
{
"id": "65f83a7ca7047318e3ccff7c",
"id": "65e62efde0592ec4b4bb6a69",
"title": "Step 35"
},
{
"id": "6352ea3a5b79e614ee2282fd",
"id": "65f83a7ca7047318e3ccff7c",
"title": "Step 36"
},
{
"id": "6352ebd3ab962c168a122e85",
"id": "6352ea3a5b79e614ee2282fd",
"title": "Step 37"
},
{
"id": "6352ec8b9c70fd17b8c7ba3f",
"id": "6352ebd3ab962c168a122e85",
"title": "Step 38"
},
{
"id": "6352ecef9f045519063da9b3",
"id": "6352ec8b9c70fd17b8c7ba3f",
"title": "Step 39"
},
{
"id": "6352edee8a4de01ad693f0e4",
"id": "6352ecef9f045519063da9b3",
"title": "Step 40"
},
{
"id": "6352ee566a59d31d24bde74b",
"id": "6352edee8a4de01ad693f0e4",
"title": "Step 41"
},
{
"id": "6352f09b1e53a420e7873344",
"id": "6352ee566a59d31d24bde74b",
"title": "Step 42"
},
{
"id": "6352f179bdca23221298a5ba",
"id": "6352f09b1e53a420e7873344",
"title": "Step 43"
},
{
"id": "6352f2526dccb523150b64fb",
"id": "6352f179bdca23221298a5ba",
"title": "Step 44"
},
{
"id": "6352f2a24eb71b24284ca2b6",
"id": "6352f2526dccb523150b64fb",
"title": "Step 45"
},
{
"id": "6352faf71a9db52631864634",
"id": "6352f2a24eb71b24284ca2b6",
"title": "Step 46"
},
{
"id": "6352fbb93a91a8272f838d42",
"id": "6352faf71a9db52631864634",
"title": "Step 47"
},
{
"id": "6352fcb156834128001ea945",
"id": "6352fbb93a91a8272f838d42",
"title": "Step 48"
},
{
"id": "6352fce75b2d3b2924930f1e",
"id": "6352fcb156834128001ea945",
"title": "Step 49"
},
{
"id": "6352fe473d53592a40ae403b",
"id": "6352fce75b2d3b2924930f1e",
"title": "Step 50"
},
{
"id": "6352fed209792d2b89e92ea1",
"id": "6352fe473d53592a40ae403b",
"title": "Step 51"
},
{
"id": "6352ff27e0e51b2c7dce0010",
"id": "6352fed209792d2b89e92ea1",
"title": "Step 52"
},
{
"id": "6352ffe4cfafa72d595a0007",
"id": "6352ff27e0e51b2c7dce0010",
"title": "Step 53"
},
{
"id": "6353004b235d7a2e0b913f2b",
"id": "6352ffe4cfafa72d595a0007",
"title": "Step 54"
},
{
"id": "6353024f5eab012fa2f57eec",
"id": "6353004b235d7a2e0b913f2b",
"title": "Step 55"
},
{
"id": "6353028147d3c7309017216a",
"id": "6353024f5eab012fa2f57eec",
"title": "Step 56"
},
{
"id": "6353028147d3c7309017216a",
"title": "Step 57 "
},
{
"id": "635302be760d6031d11a06cd",
"title": "Step 57"
"title": "Step 58"
},
{
"id": "6374249d3fbf2a5b079ba036",
"title": "Step 58"
"title": "Step 59"
}
]
}
}
@@ -7,7 +7,7 @@ dashedName: step-21
# --description--
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.
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.
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.
@@ -1,15 +1,15 @@
---
id: 635090f47eb6d9563a6fed05
title: Step 26
title: Step 27
challengeType: 0
dashedName: step-26
dashedName: step-27
---
# --description--
Now it is time to apply what you have learned to the `getMedian` function.
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.
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.
Make sure to work with the `sorted` array to find the middle numbers.
@@ -17,6 +17,11 @@ Also if you need help, refer back to the previous few steps to see how to find t
# --hints--
Your `getMedian` function should use `sorted` array.
```js
assert.equal(getMedian.toString().match(/array/g).length, 2);
```
Your `getMedian` function should return the mean of the middle two numbers if the array length is even.
@@ -123,7 +128,7 @@ const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length
--fcc-editable-region--
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
}
--fcc-editable-region--
@@ -1,8 +1,8 @@
---
id: 6352e79d15aae30fac58f48e
title: Step 27
title: Step 28
challengeType: 0
dashedName: step-27
dashedName: step-28
---
# --description--
@@ -121,11 +121,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352e80e024e89111600edfb
title: Step 28
title: Step 29
challengeType: 0
dashedName: step-28
dashedName: step-29
---
# --description--
@@ -119,11 +119,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352e93db104661305c5f658
title: Step 29
title: Step 30
challengeType: 0
dashedName: step-29
dashedName: step-30
---
# --description--
@@ -136,11 +136,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352e96d2604f813c656750b
title: Step 30
title: Step 31
challengeType: 0
dashedName: step-30
dashedName: step-31
---
# --description--
@@ -108,14 +108,14 @@ input {
```js
const getMean = array => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = array => {
const sorted = array.sort((a, b) => a - b);
const getMedian = (array) => {
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
};
}
const getMode = array => {
const counts = {};
@@ -1,8 +1,8 @@
---
id: 6352ea3a5b79e614ee2282fd
title: Step 36
title: Step 37
challengeType: 0
dashedName: step-36
dashedName: step-37
---
# --description--
@@ -130,11 +130,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352ebd3ab962c168a122e85
title: Step 37
title: Step 38
challengeType: 0
dashedName: step-37
dashedName: step-38
---
# --description--
@@ -121,11 +121,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352ec8b9c70fd17b8c7ba3f
title: Step 38
title: Step 39
challengeType: 0
dashedName: step-38
dashedName: step-39
---
# --description--
@@ -129,11 +129,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352ecef9f045519063da9b3
title: Step 39
title: Step 40
challengeType: 0
dashedName: step-39
dashedName: step-40
---
# --description--
@@ -115,11 +115,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352edee8a4de01ad693f0e4
title: Step 40
title: Step 41
challengeType: 0
dashedName: step-40
dashedName: step-41
---
# --description--
@@ -119,11 +119,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352ee566a59d31d24bde74b
title: Step 41
title: Step 42
challengeType: 0
dashedName: step-41
dashedName: step-42
---
# --description--
@@ -121,11 +121,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352f09b1e53a420e7873344
title: Step 42
title: Step 43
challengeType: 0
dashedName: step-42
dashedName: step-43
---
# --description--
@@ -113,11 +113,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352f179bdca23221298a5ba
title: Step 43
title: Step 44
challengeType: 0
dashedName: step-43
dashedName: step-44
---
# --description--
@@ -149,11 +149,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352f2526dccb523150b64fb
title: Step 44
title: Step 45
challengeType: 0
dashedName: step-44
dashedName: step-45
---
# --description--
@@ -113,11 +113,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352f2a24eb71b24284ca2b6
title: Step 45
title: Step 46
challengeType: 0
dashedName: step-45
dashedName: step-46
---
# --description--
@@ -127,11 +127,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352faf71a9db52631864634
title: Step 46
title: Step 47
challengeType: 0
dashedName: step-46
dashedName: step-47
---
# --description--
@@ -125,11 +125,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352fbb93a91a8272f838d42
title: Step 47
title: Step 48
challengeType: 0
dashedName: step-47
dashedName: step-48
---
# --description--
@@ -133,11 +133,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352fcb156834128001ea945
title: Step 48
title: Step 49
challengeType: 0
dashedName: step-48
dashedName: step-49
---
# --description--
@@ -127,11 +127,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352fce75b2d3b2924930f1e
title: Step 49
title: Step 50
challengeType: 0
dashedName: step-49
dashedName: step-50
---
# --description--
@@ -151,11 +151,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352fe473d53592a40ae403b
title: Step 50
title: Step 51
challengeType: 0
dashedName: step-50
dashedName: step-51
---
# --description--
@@ -137,11 +137,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352fed209792d2b89e92ea1
title: Step 51
title: Step 52
challengeType: 0
dashedName: step-51
dashedName: step-52
---
# --description--
@@ -115,11 +115,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352ff27e0e51b2c7dce0010
title: Step 52
title: Step 53
challengeType: 0
dashedName: step-52
dashedName: step-53
---
# --description--
@@ -113,11 +113,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6352ffe4cfafa72d595a0007
title: Step 53
title: Step 54
challengeType: 0
dashedName: step-53
dashedName: step-54
---
# --description--
@@ -127,11 +127,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6353004b235d7a2e0b913f2b
title: Step 54
title: Step 55
challengeType: 0
dashedName: step-54
dashedName: step-55
---
# --description--
@@ -136,11 +136,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6353024f5eab012fa2f57eec
title: Step 55
title: Step 56
challengeType: 0
dashedName: step-55
dashedName: step-56
---
# --description--
@@ -109,11 +109,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6353028147d3c7309017216a
title: Step 56
title: Step 57
challengeType: 0
dashedName: step-56
dashedName: step-57
---
# --description--
@@ -107,11 +107,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 635302be760d6031d11a06cd
title: Step 57
title: Step 58
challengeType: 0
dashedName: step-57
dashedName: step-58
---
# --description--
@@ -113,11 +113,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 6374249d3fbf2a5b079ba036
title: Step 58
title: Step 59
challengeType: 0
dashedName: step-58
dashedName: step-59
---
# --description--
@@ -122,11 +122,11 @@ const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length
--fcc-editable-region--
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
--fcc-editable-region--
@@ -1,8 +1,8 @@
---
id: 65aac0678d51b3f1a0cb8061
title: Step 31
title: Step 32
challengeType: 0
dashedName: step-31
dashedName: step-32
---
# --description--
@@ -115,11 +115,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 65c4dc57418fd6bfc710d61d
title: Step 32
title: Step 33
challengeType: 0
dashedName: step-32
dashedName: step-33
---
# --description--
@@ -115,11 +115,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 65ca2d0625aa3a3201067f70
title: Step 33
title: Step 34
challengeType: 0
dashedName: step-33
dashedName: step-34
---
# --description--
@@ -125,11 +125,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 65e62efde0592ec4b4bb6a69
title: Step 34
title: Step 35
challengeType: 0
dashedName: step-34
dashedName: step-35
---
# --description--
@@ -108,11 +108,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -1,8 +1,8 @@
---
id: 65f83a7ca7047318e3ccff7c
title: Step 35
title: Step 36
challengeType: 0
dashedName: step-35
dashedName: step-36
---
# --description--
@@ -108,11 +108,11 @@ input {
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
const sorted = array.toSorted((a, b) => a - b);
const median =
array.length % 2 === 0
? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]])
: sorted[Math.floor(array.length / 2)];
sorted.length % 2 === 0
? getMean([sorted[sorted.length / 2], sorted[sorted.length / 2 - 1]])
: sorted[Math.floor(sorted.length / 2)];
return median;
}
@@ -0,0 +1,133 @@
---
id: 66c395a16c3ff33dc6f8e7c0
title: Step 26
challengeType: 0
dashedName: step-26
---
# --description--
The `.sort()` method mutates the original array - in other words, it modifies the order of the elements directly. This is generally considered bad practice, as it can result in unexpected side effects.
Instead, you should use the `.toSorted()` method, which creates a new array. Change your `.sort()` call to `.toSorted()`. Do not modify the callback function.
# --hints--
You should use `.toSorted()` instead of `.sort()`.
```js
assert.notMatch(getMedian.toString(), /array\.sort/);
assert.match(getMedian.toString(), /array\.toSorted/);
```
You should not modify the callback function.
```js
assert.match(code, /array\.toSorted\(\s*\(\s*a,\s*b\s*\)\s*=>\s*a\s*-\s*b\);?/);
```
# --seed--
## --seed-contents--
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="./styles.css" />
<script src="./script.js"></script>
<title>Statistics Calculator</title>
</head>
<body>
<h1>Statistics Calculator</h1>
<p>Enter a list of comma-separated numbers.</p>
<form onsubmit="calculate(); return false;">
<label for="numbers">Numbers:</label>
<input type="text" name="numbers" id="numbers" />
<button type="submit">Calculate</button>
</form>
<div class="results">
<p>
The <dfn>mean</dfn> of a list of numbers is the average, calculated by
taking the sum of all numbers and dividing that by the count of numbers.
</p>
<p class="bold">Mean: <span id="mean"></span></p>
<p>
The <dfn>median</dfn> of a list of numbers is the number that appears in
the middle of the list, when sorted from least to greatest.
</p>
<p class="bold">Median: <span id="median"></span></p>
<p>
The <dfn>mode</dfn> of a list of numbers is the number that appears most
often in the list.
</p>
<p class="bold">Mode: <span id="mode"></span></p>
<p>
The <dfn>range</dfn> of a list of numbers is the difference between the
largest and smallest numbers in the list.
</p>
<p class="bold">Range: <span id="range"></span></p>
<p>
The <dfn>variance</dfn> of a list of numbers measures how far the values
are from the mean, on average.
</p>
<p class="bold">Variance: <span id="variance"></span></p>
<p>
The <dfn>standard deviation</dfn> of a list of numbers is the square
root of the variance.
</p>
<p class="bold">
Standard Deviation: <span id="standardDeviation"></span>
</p>
</div>
</body>
</html>
```
```css
body {
margin: 0;
background-color: rgb(27, 27, 50);
text-align: center;
color: #fff;
}
button {
cursor: pointer;
background-color: rgb(59, 59, 79);
border: 3px solid white;
color: white;
}
input {
background-color: rgb(10, 10, 35);
color: white;
border: 1px solid rgb(59, 59, 79);
}
.bold {
font-weight: bold;
}
```
```js
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
--fcc-editable-region--
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
}
--fcc-editable-region--
const calculate = () => {
const value = document.querySelector("#numbers").value;
const array = value.split(/,\s*/g);
const numbers = array.map(el => Number(el)).filter(el => !isNaN(el));
const mean = getMean(numbers);
document.querySelector("#mean").textContent = mean;
}
```