wmissimw
wmissimw

Reputation: 35

Create an array of random numbers Return highest number Javascript

I am learning JavaScript and was asked to create a solution to find and return the largest integer of an array.

let numbers = [1, 2, 3, 4, 5]

function largestNumber(numbers) {
  return Math.max.apply(null, numbers);
}

console.log(numbers);
largestNumber(numbers);

After finding the solution I wanted to expand on the question by creating a random array first then returning the highest value from it:

function randomArray(length, max) {
  return Array.apply(null, Array(length)).map(function(numbers) {
    return Math.round(Math.random() * max);
  });
}

let numbers = randomArray(5, 500)

function largestNumber(numbers) {
  return numbers.map(function(randomArray) {
    return Math.max.apply(null, randomArray);
  });
}

console.log(numbers);
largestNumber([numbers]);

While this method solved the question, it lacks the subtlety I was looking for. For instance, is there a way to nest randomArray() into largestNumber(). Is there a way to call largestNumber(numbers) instead of largestNumber([numbers]). Feedback for my solution and advice on other possible solutions would be greatly appreciated, thank you.

Upvotes: 1

Views: 647

Answers (3)

dhilt
dhilt

Reputation: 20744

Try to run this solution (ES6):

const run = (size, max) =>
  Math.max(...[...new Array(size)].map(() => Math.round(Math.random() * max)));

where size is a random array length and max is a randomizer maximum.

This could be easily split into 3 separate methods (and thanks to @Bergi for Array.from):

const getRandomList = (length, max) => 
  Array.from({length}, () => Math.round(Math.random() * max));
const getMax = (list) => Math.max(...list);
const run = (length, max) => getMax(getRandomList(length, max));

Upvotes: 2

nnnnnn
nnnnnn

Reputation: 150020

Is there a way to call largestNumber(numbers) instead of largestNumber([numbers]).

I'll cover this first because it is the easiest part: simply don't change largestNumber to use the .map() method - that's why it now expects an array of arrays, and returns an array.

Your original largestNumber() implementation expected numbers to be a one-dimensional array, and this would work on any array of numbers regardless of whether that array was populated randomly or not.

is there a way to nest randomArray() into largestNumber()

Yes. If you just want to get rid of the intermediate numbers variable you can do this:

largestNumber(randomArray(5, 500))

Or if you meant you wanted to move the random part inside the largestNumber() function body, then to have it always generate the same number of possible numbers of the same size you can just directly embed a call to randomArray(5, 500) instead of having the numbers argument:

function largestNumber() {
  return Math.max.apply(null, randomArray(5, 10));
}

Or you can make the array length and random number limit arguments of largestNumber():

function largestNumber(length, max) {
  return Math.max.apply(null, randomArray(length, max));
}

Feedback for my solution and advice on other possible solutions would be greatly appreciated

I would not embed randomArray() into largestNumber(), at least not without renaming the function to largestRandomNumber() or something. Better to keep the two functions separate so that they can be used independently.

Upvotes: 2

Andrew
Andrew

Reputation: 7545

Something like this? Embrace ES6:

const randomArray = (length, max) => {
  return Array(length).fill().map((el) =>{ 
    return Math.round(Math.random() * max)
  })
}

const largestNumber = (numbers) => {
  return Math.max(...numbers)
}

largestNumber(randomArray(5, 500))

Upvotes: 0

Related Questions