Skip to content

Commit

Permalink
refactor(curriculum): updating step 22 of stats calculator project (#…
Browse files Browse the repository at this point in the history
…54312)

Co-authored-by: Naomi <nhcarrigan@gmail.com>
Co-authored-by: Sem Bauke <semboot699@gmail.com>
  • Loading branch information
3 people committed Apr 18, 2024
1 parent f7dbd84 commit 3c1353f
Show file tree
Hide file tree
Showing 38 changed files with 666 additions and 133 deletions.
Expand Up @@ -98,140 +98,152 @@
"title": "Step 21"
},
{
"id": "635090f47eb6d9563a6fed05",
"id": "661890c4abae9f2a0eddad6b",
"title": "Step 22"
},
{
"id": "635091f8dbf554575fb5aa0c",
"id": "661899113600ef307d8a7487",
"title": "Step 23"
},
{
"id": "6352e79d15aae30fac58f48e",
"id": "6618a3264f955339063d3328",
"title": "Step 24"
},
{
"id": "6352e80e024e89111600edfb",
"id": "6618abf6368d073f0f659780",
"title": "Step 25"
},
{
"id": "6352e93db104661305c5f658",
"id": "635090f47eb6d9563a6fed05",
"title": "Step 26"
},
{
"id": "6352e96d2604f813c656750b",
"id": "6352e79d15aae30fac58f48e",
"title": "Step 27"
},
{
"id": "65aac0678d51b3f1a0cb8061",
"id": "6352e80e024e89111600edfb",
"title": "Step 28"
},
{
"id": "65c4dc57418fd6bfc710d61d",
"id": "6352e93db104661305c5f658",
"title": "Step 29"
},
{
"id": "65ca2d0625aa3a3201067f70",
"id": "6352e96d2604f813c656750b",
"title": "Step 30"
},
{
"id": "65e62efde0592ec4b4bb6a69",
"id": "65aac0678d51b3f1a0cb8061",
"title": "Step 31"
},
{
"id": "65f83a7ca7047318e3ccff7c",
"id": "65c4dc57418fd6bfc710d61d",
"title": "Step 32"
},
{
"id": "6352ea3a5b79e614ee2282fd",
"id": "65ca2d0625aa3a3201067f70",
"title": "Step 33"
},
{
"id": "6352ebd3ab962c168a122e85",
"id": "65e62efde0592ec4b4bb6a69",
"title": "Step 34"
},
{
"id": "6352ec8b9c70fd17b8c7ba3f",
"id": "65f83a7ca7047318e3ccff7c",
"title": "Step 35"
},
{
"id": "6352ecef9f045519063da9b3",
"id": "6352ea3a5b79e614ee2282fd",
"title": "Step 36"
},
{
"id": "6352edee8a4de01ad693f0e4",
"id": "6352ebd3ab962c168a122e85",
"title": "Step 37"
},
{
"id": "6352ee566a59d31d24bde74b",
"id": "6352ec8b9c70fd17b8c7ba3f",
"title": "Step 38"
},
{
"id": "6352f09b1e53a420e7873344",
"id": "6352ecef9f045519063da9b3",
"title": "Step 39"
},
{
"id": "6352f179bdca23221298a5ba",
"id": "6352edee8a4de01ad693f0e4",
"title": "Step 40"
},
{
"id": "6352f2526dccb523150b64fb",
"id": "6352ee566a59d31d24bde74b",
"title": "Step 41"
},
{
"id": "6352f2a24eb71b24284ca2b6",
"id": "6352f09b1e53a420e7873344",
"title": "Step 42"
},
{
"id": "6352faf71a9db52631864634",
"id": "6352f179bdca23221298a5ba",
"title": "Step 43"
},
{
"id": "6352fbb93a91a8272f838d42",
"id": "6352f2526dccb523150b64fb",
"title": "Step 44"
},
{
"id": "6352fcb156834128001ea945",
"id": "6352f2a24eb71b24284ca2b6",
"title": "Step 45"
},
{
"id": "6352fce75b2d3b2924930f1e",
"id": "6352faf71a9db52631864634",
"title": "Step 46"
},
{
"id": "6352fe473d53592a40ae403b",
"id": "6352fbb93a91a8272f838d42",
"title": "Step 47"
},
{
"id": "6352fed209792d2b89e92ea1",
"id": "6352fcb156834128001ea945",
"title": "Step 48"
},
{
"id": "6352ff27e0e51b2c7dce0010",
"id": "6352fce75b2d3b2924930f1e",
"title": "Step 49"
},
{
"id": "6352ffe4cfafa72d595a0007",
"id": "6352fe473d53592a40ae403b",
"title": "Step 50"
},
{
"id": "6353004b235d7a2e0b913f2b",
"id": "6352fed209792d2b89e92ea1",
"title": "Step 51"
},
{
"id": "6353024f5eab012fa2f57eec",
"id": "6352ff27e0e51b2c7dce0010",
"title": "Step 52"
},
{
"id": "6353028147d3c7309017216a",
"id": "6352ffe4cfafa72d595a0007",
"title": "Step 53"
},
{
"id": "635302be760d6031d11a06cd",
"id": "6353004b235d7a2e0b913f2b",
"title": "Step 54"
},
{
"id": "6374249d3fbf2a5b079ba036",
"id": "6353024f5eab012fa2f57eec",
"title": "Step 55"
},
{
"id": "6353028147d3c7309017216a",
"title": "Step 56"
},
{
"id": "635302be760d6031d11a06cd",
"title": "Step 57"
},
{
"id": "6374249d3fbf2a5b079ba036",
"title": "Step 58"
}
]
}
@@ -1,46 +1,35 @@
---
id: 635090f47eb6d9563a6fed05
title: Step 22
title: Step 26
challengeType: 0
dashedName: step-22
dashedName: step-26
---

# --description--

The next step is to find the number in the middle of the list. If the list has an odd number of numbers, the middle number is the median. If the list has an even number of numbers, the median is the average of the two middle numbers.
Now it is time to apply what you have learned to the `getMedian` function.

You can check if a number is even or odd with the <dfn>modulus operator</dfn>, which is represented by the `%` symbol. This operator returns the remainder of the division of two numbers. If the remainder is `0`, the number is even. If the remainder is `1`, the number is odd:
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.

```js
array.length % 2 === 0;
```
Make sure to work with the `sorted` array to find the middle numbers.

Declare a `median` variable. Using the ternary operator, check if the length of `array` is even. If the length of `array` is even, find the two middle numbers and calculate the mean of those numbers. If the length of `array` is odd, find the middle number and assign it to the `median` variable.
Also if you need help, refer back to the previous few steps to see how to find the median for an array.

# --hints--

Your `getMedian` function should have a `median` variable.

```js
assert.match(getMedian.toString(), /median\s*=/);
```

Your `median` variable should use a ternary operator to check if the `array.length` is even.

```js
assert.match(getMedian.toString(), /median\s*=\s*array\.length\s*%\s*2\s*===\s*0\s*\?/);
```

If `array.length` is even, pass an array with the two middle numbers from the `sorted` array to the `getMean` function. The first item in the array argument should be the value at index `array.length / 2` from `sorted` and the second should use the value at the index `array.length / 2 - 1` from `sorted`.
Your `getMedian` function should return the mean of the middle two numbers if the array length is even.

```js
assert.match(getMedian.toString(), /median\s*=\s*array\.length\s*%\s*2\s*===\s*0\s*\?\s*getMean\(\s*\[\s*sorted\s*\[\s*array\.length\s*\/\s*2\s*\]\s*,\s*sorted\s*\[\s*array\.length\s*\/\s*2\s*-\s*1\s*\]\s*\]\s*\)\s*\:/);
assert.equal(getMedian([1,2,3,4,5,6]), 3.5);
assert.equal(getMedian([1,2,3,4,5,6,7,8,9,10]), 5.5);
```

If the `array.length` is odd, your `median` variable should assign the middle number to the `median` variable. The middle number is the value of `sorted` at `Math.floor(array.length / 2)`.
Your `getMedian` function should return the middle number if the array length is odd.

```js
assert.match(getMedian.toString(), /median\s*=\s*array\.length\s*%\s*2\s*===\s*0\s*\?\s*getMean\(\s*\[\s*sorted\s*\[\s*array\.length\s*\/\s*2\s*\]\s*,\s*sorted\s*\[\s*array\.length\s*\/\s*2\s*-\s*1\s*\]\s*\]\s*\)\s*\:\s*sorted\s*\[\s*Math\.floor\(\s*array\.length\s*\/\s*2\s*\)\s*\]\s*;/);
assert.equal(getMedian([1,2,3,4,5]), 3);
assert.equal(getMedian([1,2,3,4,5,6,7,8,9]), 5);
```

# --seed--
Expand Down
@@ -1,8 +1,8 @@
---
id: 6352e79d15aae30fac58f48e
title: Step 24
title: Step 27
challengeType: 0
dashedName: step-24
dashedName: step-27
---

# --description--
Expand Down
@@ -1,8 +1,8 @@
---
id: 6352e80e024e89111600edfb
title: Step 25
title: Step 28
challengeType: 0
dashedName: step-25
dashedName: step-28
---

# --description--
Expand Down
@@ -1,8 +1,8 @@
---
id: 6352e93db104661305c5f658
title: Step 26
title: Step 29
challengeType: 0
dashedName: step-26
dashedName: step-29
---

# --description--
Expand Down
@@ -1,8 +1,8 @@
---
id: 6352e96d2604f813c656750b
title: Step 27
title: Step 30
challengeType: 0
dashedName: step-27
dashedName: step-30
---

# --description--
Expand Down
@@ -1,8 +1,8 @@
---
id: 6352ea3a5b79e614ee2282fd
title: Step 33
title: Step 36
challengeType: 0
dashedName: step-33
dashedName: step-36
---

# --description--
Expand Down
@@ -1,8 +1,8 @@
---
id: 6352ebd3ab962c168a122e85
title: Step 34
title: Step 37
challengeType: 0
dashedName: step-34
dashedName: step-37
---

# --description--
Expand Down
@@ -1,8 +1,8 @@
---
id: 6352ec8b9c70fd17b8c7ba3f
title: Step 35
title: Step 38
challengeType: 0
dashedName: step-35
dashedName: step-38
---

# --description--
Expand Down
@@ -1,8 +1,8 @@
---
id: 6352ecef9f045519063da9b3
title: Step 36
title: Step 39
challengeType: 0
dashedName: step-36
dashedName: step-39
---

# --description--
Expand Down
@@ -1,8 +1,8 @@
---
id: 6352edee8a4de01ad693f0e4
title: Step 37
title: Step 40
challengeType: 0
dashedName: step-37
dashedName: step-40
---

# --description--
Expand Down
@@ -1,8 +1,8 @@
---
id: 6352ee566a59d31d24bde74b
title: Step 38
title: Step 41
challengeType: 0
dashedName: step-38
dashedName: step-41
---

# --description--
Expand Down
@@ -1,8 +1,8 @@
---
id: 6352f09b1e53a420e7873344
title: Step 39
title: Step 42
challengeType: 0
dashedName: step-39
dashedName: step-42
---

# --description--
Expand Down
@@ -1,8 +1,8 @@
---
id: 6352f179bdca23221298a5ba
title: Step 40
title: Step 43
challengeType: 0
dashedName: step-40
dashedName: step-43
---

# --description--
Expand Down
@@ -1,8 +1,8 @@
---
id: 6352f2526dccb523150b64fb
title: Step 41
title: Step 44
challengeType: 0
dashedName: step-41
dashedName: step-44
---

# --description--
Expand Down
@@ -1,8 +1,8 @@
---
id: 6352f2a24eb71b24284ca2b6
title: Step 42
title: Step 45
challengeType: 0
dashedName: step-42
dashedName: step-45
---

# --description--
Expand Down

0 comments on commit 3c1353f

Please sign in to comment.