Hello,
I’ve tried with if statements and ternary operators but I cannot manage to pass this step and I don’t understand what’s going wrong.
Please help.
Your code so far
<!-- file: index.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>
/* file: script.js */
const getMean = (array) => array.reduce((acc, el) => acc + el, 0) / array.length;
// User Editable Region
const getMedian = (array) => {
const sorted = array.toSorted((a, b) => a - b);
const median = "";
if (sorted.length % 2 === 0) {
median = getMean([sorted[sorted.length/2], sorted[(sorted.length/2) - 1]]);
} else if (sorted.length % 2 === 1) {
median = sorted[Math.floor(sorted.length/2)];
}
return median;
}
// User 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;
}
Did you change your code since you posted it? If so, please post your updated code. That error message did not show up in the console when I entered your code.
// running tests
2. Your getMedian function should return the mean of the middle two numbers if the array length is even.
3. Your getMedian function should return the middle number if the array length is odd.
// tests completed
// console output
[TypeError: "median" is read-only]
[TypeError: "median" is read-only]
Okay. Let’s move past the array.toSorted TypeError. I don’t know why you’re seeing that. When I enter your code, I see the error message you should be paying attention to because that’s where your code is broken: [TypeError: "median" is read-only] Please look carefully at your code. Can you see why that error message is displaying?
Hm, it’s a bit weird I can’t reproduce that. Obviously it is a function.
Did you already try resetting this step?
Please try one of the following steps to move forward.
Click on the “Reset” step button and force a refresh of your page with CTRL + F5 if you’re on Windows then try to paste the code in again. On other systems, please follow the instructions here.
or - Try the step in incognito or private mode.
or - Disable any/all extensions that interface with the freeCodeCamp website (such as Dark Mode, Ad Blockers, or Spellcheckers), and set your browser zoom level to 100%. Both of these factors can cause tests to fail erroneously.
or - Ensure your browser is up-to-date or try a different browser.
or - Turn off high contrast themes on Windows (from accessibility settings menu)
Yep, one of the solutions worked.
I’ve tried disabling dark mode, tried the incognito mode in Chrome, then Chrome Dev and then, where everything else failed, I tried Firefox and I did pass with the originary posted code.
If you passed with the original posted code, something is wrong with the tests, because median was declared as a constant, meaning you cannot change the initial value.