“Retourner les plus grands nombres dans les tableaux” Réponses codées

FreeCodeCamp Basic Algorithm Scripting Renvoie les plus grands nombres dans les tableaux

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

Retourner les plus grands nombres dans les tableaux

// 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

Retourner les plus grands nombres dans les tableaux

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

Trouvez le plus grand nombre d'un tableau

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

Réponses similaires à “Retourner les plus grands nombres dans les tableaux”

Questions similaires à “Retourner les plus grands nombres dans les tableaux”

Plus de réponses similaires à “Retourner les plus grands nombres dans les tableaux” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code