1. Write a JavaScript program to generate n number of random numbers and store them
    in an array. Sort the generated numbers in ascending order using array sort method.
    Develop separate functions to find mean and median of numbers that are in the array.
    Display the results with appropriate messages.

Dani AI

Generated

posted a programming exercise and correctly reminded that showing effort is good practice. The following is a compact, practical example with common pitfalls and verification tips useful for anyone implementing a small statistics helper in JavaScript.

function generateRandomInts(n, min = 0, max = 100) {
  if (n <= 0) return [];
  return Array.from({ length: n }, () =>
    Math.floor(Math.random() * (max - min + 1)) + min
  );
}

function mean(arr) {
  if (!arr.length) return NaN;
  return arr.reduce((s, v) => s + v, 0) / arr.length;
}

function median(arr) {
  if (!arr.length) return NaN;
  const a = arr.slice().sort((x, y) => x - y);
  const mid = Math.floor(a.length / 2);
  return (a.length % 2) ? a[mid] : (a[mid - 1] + a[mid]) / 2;
}

Notes and gotchas: Array.prototype.sort defaults to lexicographic ordering, so a numeric comparator is required (Array.prototype.sort). Math.random is not seedable for repeatable tests; deterministic runs can use a seeded PRNG such as seedrandom (https://github.com/davidbau/seedrandom) or cryptographic randomness via crypto.getRandomValues (getRandomValues). Handle empty arrays explicitly, decide whether results should be integers or floats, and remember that sort mutates the array (use a copy when the original order must be preserved).

Dumping an assignment to a post without at least saying that is what you are doing isn't the most polite way to start a conversation, especially if you don't show any of your own effort to solve the problem.

It also isn't the safest approach, since plenty of professors regularly search the web to catch students doing things such as this (or more often, they get their TAs and grad students to do it for them).

There are websites where they will do your homework for you, at least for a fee. This isn't one of those sites.

So, try to solve it yourself, and if you get stuck, let us know what you've tried and we'll see if we can help you then.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.