red_panda
red_panda

Reputation: 344

How to find a largest number in an array?

function myarray(min, max) {
  var points = [];
  for (var i = 0; i < 10; i++) {
    points.push(Math.round(Math.random() * (1000 - 100 + 1) + 100));
    points.join('<br>');
    var largest = Math.max.apply(0, points);
  }
  return points
}

console.log(myarray());

My task is pretty simple, I want to create 10 random numbers from 1 to 1000 in an array and then print the highest one. I think I am pretty close but when I run this I get undefined.

How can I fix this? and what is undefined?

Upvotes: 1

Views: 2503

Answers (6)

KooiInc
KooiInc

Reputation: 122906

Math.max.call(...[some array of numbers]) will give you the largest number of the array. In your code min and max are not used at all. Here is 1/ an alternative way of creating an array (with length len) of numbers between min and max and 2/ the application of Math.max.

You may want to consider that there may be multiple largest number within the array (because the generated random values are not necessarily unique). See this Stackblitz snippet if you need unique random numbers.

function randomNr(min, max) {
  return Math.floor( ([...crypto.getRandomValues(new Uint32Array(1))].shift() / 
    (2 ** 32) ) * (max - min + 1) + min );
}

function myArray(min, max, len = 10) {
  return [...Array(len)].map(_ => randomNr(min, max) );
}

for (let i = 0; i < 5; i += 1) {
  const arr = myArray(10, 50, 5);
  console.log( `${JSON.stringify(arr.sort( (a, b) => a-b))}, max: ${
    Math.max.call(...arr)}` );
}

// probable multiple maximum values (run snippet multiple times)
for (let i = 0; i < 5; i += 1) {
  const arr = myArray(1, 5, 10);
  console.log( `${JSON.stringify(arr.sort( (a, b) => a-b))}, max: ${
    Math.max.call(...arr)}` );
}
.as-console-wrapper {
    max-height: 100% !important;
}

Upvotes: 0

Vishal
Vishal

Reputation: 11

Python code

Brute force:

arr.sort()
return arr[len(arr)-1]

Optimal:

import sys
largest_element = -sys.maxsize - 1 # equal to INT_MIN

for i in arr:

    if i > largest_element:
        largest_element = i
return largest_element

If you want understand this concept in tamil, You can checkout the below link

https://www.instagram.com/reel/C2pUF75vZBC/?igsh=M2dxMzhyMm9yeXpo

Upvotes: 1

Bhautik
Bhautik

Reputation: 11282

You can compare points to the largest number. Try the below snippet.

function myarray(min, max) {
    var points = [];
    var largest = 0;
    for (var i = 0; i < 10; i++) {    
        points.push(Math.round(Math.random() * (1000 - 100 + 1) + 100));
        if ( points[i] > largest ) {
            var largest = points[i];
        }
    }
    console.log(points);
    console.log(largest);
}
myarray();

As per OP comment.

function myarray(min, max) {
    var points = [ 521,338,761,834,561,842,177,862,173 ];
    var largest = 0;
    for (var i = 0; i < 10; i++) {    
        if ( points[i] > largest ) {
            var largest = points[i];
        }
    }
    console.log(points);
    console.log(largest);
}
myarray();

Upvotes: 2

Emanuele
Emanuele

Reputation: 380

Because I love one-line solutions :)

console.log(Math.max(...Array.apply(null, Array(10)).map(
      x=>Math.round(Math.random() * (1000 - 100 + 1) + 100)
    )))

Upvotes: 0

Jamiec
Jamiec

Reputation: 136104

All you need to do is move the call to Math.max.apply(0, points) outside the loop which is building the array and return that. Also, no need for the points.join line at all

function myarray(min, max) {
  var points = [];
  for (var i = 0; i < 10; i++) {
    points.push(Math.round(Math.random() * (1000 - 100 + 1) + 100));
  }
  var largest = Math.max.apply(null, points);
  return largest
}

console.log(myarray());

Upvotes: 2

XGG
XGG

Reputation: 47

let arr = [1,2,3,5,25,35,43,75,100,35,3244,345,535,532]
    let maxnum = arr.reduce((pre,cur) =>{
        if(pre <= cur){
            pre = cur
        }
        return pre
    },0)

Upvotes: 0

Related Questions