##### finding the largest number in an array
DamienVapes

I have some code im having trouble with if someone could give me an idea or an answer id be greatly in your debt.

function getLargestNumber(numbers) {

}

/ Do not modify code below this line /

const largestNumber = getLargestNumber([1, 9, 5]);
console.log(largestNumber, '<-- should be 9');

You are viewing a single comment. View All
LeonDoesCode

In JavaScript, you can use the `Math.max.apply()` function to do this, like so:

```.css-o0gntx{position:absolute;left:-9999px;}.css-4kcfpn{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}// Will return the largest number
function getLargestNumber(numbers) {
// Takes in Math and the array of numbers, and returns the largest one
return Math.max.apply(Math, numbers);
}

// Or
function getLargestNumber(numbers) {
return Math.max(...numbers);
}```

This is like calling:

```.css-o0gntx{position:absolute;left:-9999px;}.css-4kcfpn{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}function getLargestNumber() {
// This function returns the largest number from all the numbers parsed in
return Math.max(5, 7, 3, 2);
}```

You can also do this with `Math.min` too.

Hope this is what you were looking for!

theangryepicbanana

@LeonDoesCode In the future, please do not give direct answers. Instead, try to help them understand how to do it

LeonDoesCode

@theangryepicbanana That is why I also included the second part (after "This is like calling:"). It explains what that function is similar to calling, and how that function works.

However, I see your point, will try to not do this in future. Thanks for the feedback!

DamienVapes

@LeonDoesCode thank you i was over complicating this..i was trying to put in a for loop and everything lol

LeonDoesCode

@DamienVapes Gald it helped!

DamienVapes

@LeonDoesCode how did you post the code the way you did on your earlier comment.id like to show my repl completed here

LeonDoesCode

@DamienVapes You can post code like so:

```.css-o0gntx{position:absolute;left:-9999px;}.css-4kcfpn{-webkit-align-items:stretch;-webkit-box-align:stretch;-ms-flex-align:stretch;align-items:stretch;border-width:0;border-style:solid;box-sizing:border-box;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-basis:auto;-ms-flex-preferred-size:auto;flex-basis:auto;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-flex-shrink:0;-ms-flex-negative:0;flex-shrink:0;outline:none;min-height:0;min-width:0;position:absolute;left:-9999px;}Use these at the beginning and end ```
You can also put a language name on the first one e.g  ```python```