1

I working with array, and i have array with numbers:

var array = [0,10,12,23,56,111,35,66,77,124,54];

As u can see i have number, from this numbers is bigger 124, and i want to return this number.

i try to create some func:

 var bigestValue = array.reduce(function (a, b) { return a > b ? a : b; })
if (bigestValue <= 100) {
        return 100;
    } 

But when script is updating it's return for me last values

8
  • What's the expected output? Commented Jan 23, 2020 at 12:36
  • An array is a data type of JavaScript. Why would you want to use jQuery for that? Just use a simple for loop. Commented Jan 23, 2020 at 12:36
  • 2
    [0,10,12,23,56,111,35,66,77,124,54].find(num => num > 100); Commented Jan 23, 2020 at 12:36
  • 2
    [0,10,12,23,56,111,35,66,77,124,54].sort((a, b) => a > b ? -1 : 1)[0] Commented Jan 23, 2020 at 12:39
  • 1
    [0,10,12,23,56,111,35,66,77,124,54].reduce((max, num) => num > max ? num : max, Number.MIN_SAFE_INTEGER) Commented Jan 23, 2020 at 12:41

1 Answer 1

1

You can get Max number from array using

var array = [0,10,12,23,56,111,35,66,77,124,54];
var max = array.reduce((a,b)=> a > b ? a : b,0);

console.log(max);

If you want to filter array by getting only elements bigger than 100, then

var array = [0,10,12,23,56,111,35,66,77,124,54];
var maxArr = array.filter((a)=> a > 100);

console.log(maxArr);

Sign up to request clarification or add additional context in comments.

3 Comments

Glad to help, Please mark the answer as accepted so it would help others facing similar issue.
Not only did you just repeat what I have already said in the comments, but your solution doesn't even work for negative numbers.
With all due respect, I did post an answer as requested by the author, Nowhere it was mentioned that he wanted this to work for negative numbers, It's approved from author for a reason

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.