How Might I Find the Largest Number Contained in a JavaScript Array

How might I find the largest number contained in a JavaScript array?

Resig to the rescue:

Array.max = function( array ){
return Math.max.apply( Math, array );
};

Warning: since the maximum number of arguments is as low as 65535 on some VMs, use a for loop if you're not certain the array is that small.

Finding largest integer in an array in JavaScript

The code below is fixed and should work. The problem was that in this line if (array>largest) { You were not providing the index of the array. By changing the code to this if (array[i]>largest) { it works. Notice that I added the [i] to the end of array in the if statement.

var array = [3 , 6, 2, 56, 32, 5, 89, 32];
var largest= 0;

for (i=0; i<array.length; i++){
if (array[i]>largest) {
largest=array[i];
}
}

console.log(largest);

Find the biggest number in an array by using JavaScript loops

zer00ne's answer should be better for simplicity, but if you still want to follow the for-loop way, here it is:

function biggestNumberInArray (arr) {
// The largest number at first should be the first element or null for empty array
var largest = arr[0] || null;

// Current number, handled by the loop
var number = null;
for (var i = 0; i < arr.length; i++) {
// Update current number
number = arr[i];

// Compares stored largest number with current number, stores the largest one
largest = Math.max(largest, number);
}

return largest;
}

How to find a largest number in an array?

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();

Javascript how to find largest numbers in array, and record positions

This is so close to Return index of greatest value in an array.

But if you want to get all indexes, you can just iterate the array, pushing the current index to an argmax array when the current item is equal to max, the maximum number found until the moment; or update max and argmax when you find a greater item.

var max = -Infinity, argmax = [];
for(var i=0; i<array.length; ++i)
if(array[i] > max) max = array[i], argmax = [i];
else if(array[i] === max) argmax.push(i);
argmax; // [2,3,4]

Find largest number in array of arrays

Since you'd like to use a for loop:

var largestOfFour = (arr) => {
let largest = [];
for(let i = 0; i < arr.length; i++) {
largest[i] = arr[i].sort((a,b) => b-a)[0];
}
return largest;
}

What this will do is sort the inner arrays and grab the first one out of there.

Return index of greatest value in an array

This is probably the best way, since it’s reliable and works on old browsers:

function indexOfMax(arr) {
if (arr.length === 0) {
return -1;
}

var max = arr[0];
var maxIndex = 0;

for (var i = 1; i < arr.length; i++) {
if (arr[i] > max) {
maxIndex = i;
max = arr[i];
}
}

return maxIndex;
}

There’s also this one-liner:

let i = arr.indexOf(Math.max(...arr));

It performs twice as many comparisons as necessary and will throw a RangeError on large arrays, though. I’d stick to the function.

javascript - Find biggest number from 2 arrays inputted by user

You can use Math.max.apply for getting the maximum value inside the array.

function getResult(){var Answer = largestNumbers(document.getElementById("firstArray").value,         document.getElementById("secondArray").value);console.log(Answer);function largestNumbers(arr, arr1) {  var largeList = [];  var otherArray = [];  otherArray[0] = arr.split(" ").map(x=>{return parseInt(x)});  otherArray[1] = arr1.split(" ").map(x=>{return parseInt(x)});  debugger  largeList.push(Math.max.apply(null, otherArray[0]));  largeList.push(Math.max.apply(null, otherArray[1]));    return largeList; }}
<input type = "text" id = "firstArray"><br/><input type = "text" id = "secondArray"><br><button id='result' onclick='getResult()'>Get Largest</button>

Extract largest numbers from an array

Aternative way of choosing largest numbers:

var set = new Array(1, 2, 1, 2, 3, 9, 12, 15);

function getHighNums(arr,percentage,fillpercentage){
var perc=0;
var sorted=arr.sort(function(a,b){ return a-b});
var total=0;
for(var i=0;i<arr.length;i++) total+=arr[i];
for(var j=sorted.length-1;j>=0;j--){
perc+=sorted[j]/total*100;
if(fillpercentage){
if(perc > percentage) return sorted.slice(j,sorted.length);
}else{
if(sorted[j]/total*100 < percentage) return sorted.slice(j+1,sorted.length);
}
}
return sorted;

}

console.log(getHighNums(set, 10, false))//9,12,15
console.log(getHighNums(set, 50, true))//12,15

The first line gets all numbers that are at least 10% of sum of array value. Sum of array= 1+2+1+2+3+9+12+15=45 so it picks numbers > 4.5

The second line gets numbers until their sum is at least 50% of the total sum of the array. So as sum of array =45, it will pick highest numbers until their sum is > 22.5



Related Topics



Leave a reply



Submit