Co-authored-by: Naomi <nhcarrigan@gmail.com> Co-authored-by: Sem Bauke <semboot699@gmail.com>
4.7 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6618abf6368d073f0f659780 | Step 25 | 0 | step-25 |
--description--
Now that you have a better understanding of how to find the median for odd and even lists of numbers, you can remove all your test code from the previous steps.
--hints--
You should not have a const testArr1 = [1, 2, 3, 4, 5]; in your code.
assert.notMatch(code, /const testArr1 = \[1, 2, 3, 4, 5\];/);
You should not have a const testArr2 = [1, 2, 3, 4, 5, 6]; in your code.
assert.notMatch(code, /const testArr2 = \[1, 2, 3, 4, 5, 6\];/);
You should not have a const isEven = testArr2.length % 2 === 0; in your code.
assert.notMatch(code, /const isEven = testArr2.length % 2 === 0;/);
You should not have a console.log(isEven); in your code.
assert.notMatch(code, /console\.log\(isEven\);/);
You should not have a const oddListMedian = testArr1[Math.floor(testArr1.length / 2)]; in your code.
assert.notMatch(code, /const oddListMedian = testArr1\[Math\.floor\(testArr1\.length \/ 2\)\];/);
You should not have a console.log(oddListMedian); in your code.
assert.notMatch(code, /console\.log\(oddListMedian\);/);
You should not have a const evenListMedian = getMean([testArr2[testArr2.length / 2 - 1], testArr2[testArr2.length / 2]]); in your code.
assert.notMatch(code, /const evenListMedian = getMean\(\[testArr2\[testArr2\.length \/ 2 - 1\], testArr2\[testArr2\.length \/ 2\]\]\);/);
You should not have a console.log(evenListMedian); in your code.
assert.notMatch(code, /console\.log\(evenListMedian\);/);
--seed--
--seed-contents--
<!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>
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;
}
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
--fcc-editable-region--
const testArr1 = [1, 2, 3, 4, 5];
const testArr2 = [1, 2, 3, 4, 5, 6];
const isEven = testArr2.length % 2 === 0;
console.log(isEven);
const oddListMedian = testArr1[Math.floor(testArr1.length / 2)];
console.log(oddListMedian);
const evenListMedian = getMean([testArr2[testArr2.length / 2 - 1], testArr2[testArr2.length / 2]]);
console.log(evenListMedian);
--fcc-editable-region--
const getMedian = (array) => {
const sorted = array.sort((a, b) => a - b);
}
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;
}