1

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?

Nguyễn Văn Phong
  • 13,506
  • 17
  • 39
  • 56
red_panda
  • 364
  • 2
  • 18
  • 2
    1. You're printing all numbers instead of just the highest one. 2. You are calculating the largest number at each step when adding a number to the array - that's unnecessary. 3. `points.join()` does nothing since it returns a new value which is not stored anywhere. – Tsvetan Ganev Apr 26 '21 at 06:55
  • Click https://stackoverflow.com/questions/1669190/find-the-min-max-element-of-an-array-in-javascript – girishf15 Apr 26 '21 at 06:55

4 Answers4

2

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());
Jamiec
  • 133,658
  • 13
  • 134
  • 193
2

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();
Bhautik
  • 11,125
  • 3
  • 16
  • 38
0
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)
XGG
  • 51
  • 4
  • @Jamiec: try it with 500000 items ;) – georg Apr 26 '21 at 08:13
  • @georg, ok for a very large set of numbers `Math.max.apply` will not work at all. But for a slightly shorter set of numbers this way is [~80% slower](https://jsbench.me/crknyc2865/1) – Jamiec Apr 26 '21 at 08:23
  • @Jamiec: sure, reduce is the wrong tool here, but how about a simple `for` loop? – georg Apr 26 '21 at 08:28
  • @georg for once the OP chose the right solution (the for loop). BTW I should add the user was creating a list of just 10 numbers! It doesnt matter which solution you use with that number of items :D – Jamiec Apr 26 '21 at 08:29
0

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)
    )))
Emanuele
  • 382
  • 3
  • 9
  • Since you like OLS's and spread syntax why not consequently following one style ... `Math.max(...[...Array(10)].map( /*...*/ ))` then? – Peter Seliger Apr 26 '21 at 11:34