“Mengembalikan angka terbesar dalam array” Kode Jawaban

FreeCodecamp Algorithma Basic Scripting Return Jumlah terbesar dalam array

function largestOfFour(arr) {
  var newArr = [];
for(var i = arr.length - 1; i >= 0; i--){
  arr[i].sort(function(a, b){return a - b});
newArr.push(arr[i][arr.length - 1])
}
 newArr.sort(function(a, b){return -b - a});
  return newArr;
}
Tired Tern

Mengembalikan angka terbesar dalam array

// Return Largest Numbers in Arrays

function largestOfFour(arr) {
	let largestArr = [];
	for (let i = 0; i < arr.length; i++) {
		largestArr.push(Math.max(...arr[i]));
	}
	return largestArr;
}

largestOfFour([
	[4, 5, 1, 3],
	[13, 27, 18, 26],
	[32, 35, 37, 39],
	[1000, 1001, 857, 1],
]);

// OR

function largestOfFour(arr) {
	return arr.map(function (group) {
		return group.reduce(function (prev, current) {
			return current > prev ? current : prev;
		});
	});
}
YosKa

Mengembalikan angka terbesar dalam array

function largestOfFour(arr) {
  let result = []
  for (let i = 0; i < arr.length; i++) {
    let largest = arr[i][0]
    for (let j = 0; j < arr.length; j++) {
      if(arr[i][j] > largest) {
        largest = arr[i][j];
      }
    }
    result[i] = largest;
  }
  return result;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]))

// Explanations:

/*  Create a variable to store the results as an array.
    Create an outer loop to iterate through the outer array.
    Create a second variable to hold the largest number and initialise it with the first number. This must be outside an inner loop so it won’t be reassigned until we find a larger number.
    Create said inner loop to work with the sub-arrays.
    Check if the element of the sub array is larger than the currently stored largest number. If so, then update the number in the variable.
    After the inner loop, save the largest number in the corresponding position inside of the results array.
    And finally return said array. 
*/
Ralph Dizon

Temukan nomor terbesar dari array

static int LargestNumberin(int[] numbers){
            int largestNumber = numbers[0];
            for (int i = 1; i < numbers.Length +1; i++)
            {
                if(numbers[i] > largestNumber){
                    largestNumber = numbers[i];
                }
            }
            return largestNumber;
        }
Energetic Eagle

Jawaban yang mirip dengan “Mengembalikan angka terbesar dalam array”

Pertanyaan yang mirip dengan “Mengembalikan angka terbesar dalam array”

Lebih banyak jawaban terkait untuk “Mengembalikan angka terbesar dalam array” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya