How to create a mean, median, and mode calculator?

Good evening! I’ve attempted to successfully create this calculator but I didn’t get anywhere… I’m having trouble with this calculator! Any help? :grin:

Looks like @Paul_Wilkins has answered this previously.

Try a search in the future for broad questions like this :+1:

I’ve already tried it, it doesn’t work in the coding editor I’m using. I’m using Brackets.io

What doesn’t work in Brackets, Paul’s functions?

Yes.

Sorry, I have no idea what you mean. This code runs in a browser, and will open in any text editor.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="text/javascript">
function mean(numbers) {
  // mean of [3, 5, 4, 4, 1, 1, 2, 3] is 2.875
  var total = 0,
      i;
  for (i = 0; i < numbers.length; i += 1) {
      total += numbers[i];
  }
  return total / numbers.length;
}
function median(numbers) {
  // median of [3, 5, 4, 4, 1, 1, 2, 3] = 3
  var median = 0,
      numsLen = numbers.length;
  numbers.sort();
  if (numsLen % 2 === 0) { // is even
      // average of two middle numbers
      median = (numbers[numsLen / 2 - 1] + numbers[numsLen / 2]) / 2;
  } else { // is odd
      // middle number only
      median = numbers[(numsLen - 1) / 2];
  }
  return median;
}
function mode(numbers) {
  // as result can be bimodal or multimodal,
  // the returned result is provided as an array
  // mode of [3, 5, 4, 4, 1, 1, 2, 3] = [1, 3, 4]
  var modes = [],
      count = [],
      i,
      number,
      maxIndex = 0;
  for (i = 0; i < numbers.length; i += 1) {
      number = numbers[i];
      count[number] = (count[number] || 0) + 1;
      if (count[number] > maxIndex) {
          maxIndex = count[number];
      }
  }
  for (i in count) if (count.hasOwnProperty(i)) {
      if (count[i] === maxIndex) {
          modes.push(Number(i));
      }
  }
  return modes;
}
function range(numbers) {
  // range of [3, 5, 4, 4, 1, 1, 2, 3] is [1, 5]
  numbers.sort();
  return [numbers[0], numbers[numbers.length - 1]];
}

var data = [3, 5, 4, 4, 1, 1, 2, 3];
console.log(mean(data));
console.log(median(data));
console.log(mode(data));
console.log(range(data));
</script>
</body>
</html>

Ok, I inputted the code that you just replied with into Brackets but it doesn’t show up when I click Live Preview (The lightning symbol in the top right).

That code just logs the output in the console.
The UI for a calcluator you’ll need to build yourself in HTML and CSS.

Ok then, that’s what I’m having problems with. Sorry for not getting to the point :expressionless:

You’re wanting to build something like this?

https://www.google.com.au/search?q=calculator

You either want to use tables or a css grid for layout, you can ask in the CSS forum if you’re struggling with the layout side of things.
You’re going to need to try and make start and post back here if you have trouble.

No, I just want to make a program that calculates mean median and mode. :confused:

The first example that I provided is a program that calculates mean, median and mode. Run it in a browser, open up up the console and Bam. Calculated values.

Once that’s calculated what do you want to do with it?

When I run the sample that you first inputted into Google Chrome it just shows a blank white screen.

Ok, I’ve tried answering it a couple of times.

That code just logs the output in the console.
The UI for a calcluator you’ll need to build yourself in HTML and CSS.

To open up the console you can right click the page > Inspect and then hit the Console tab.
Reload and you should see the output of the program.

To see something on the page you’ll need HTML and CSS like I’ve said, but then you replied with:

No, I just want to make a program that calculates mean median and mode.

My bad.

Ok, I need help with adding the HTML and the CSS. :confused:

Make a start with an <input> and <button>'s for the different things in the calculator.

MDN is the best resource for info about web tech, you might need to follow https://developer.mozilla.org/en-US/docs/Web/Guide/HTML if you’re just starting out.

23 posts were split to a new topic: What’s more important in JS: performance, or the understandability of the code?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.