Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
How to find the highest number in a 2-dimensional array?

Hi all and thanks in advance for your help.

So I would like to find the highest number in a 2-dimensional array. Below is the code:

const matrix = [
  [2, 56, 10],
  [20, 34, 10],
  [23, 144, 26]
];
let maximum = matrix[0][0];
for (var row = 0; row < matrix.length; row++) {
  for (var col = 0; col < matrix.length; col++) {
    if (matrix[row][col] > maximum) {
      maximum = matrix[row][col];
    };
  };
};
document.write(' -- ', maximum);

Here is my problem - Could you please help me to understand why when I have more numbers in the array I cannot see the highest number - Find below an example ):

const matrix = [
  [2, 56, 10, 14, 422, 3242],
  [20, 34, 55, 100, 33, 422],
  [23, 12, 14, 26, 203, 233]
];
let maximum = matrix[0][0];
for (var row = 0; row < matrix.length; row++) {
  for (var col = 0; col < matrix.length; col++) {
    if (matrix[row][col] > maximum) {
      maximum = matrix[row][col];
    };
  };
};
document.write(' -- ', maximum);

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

row < matrix.length tests the correct thing. col < matrix.length does not: you should replace it with col < matrix[row].length.

However, there is an easier way, using some of the newer JavaScript features:

const matrix = [[2,56,10,14,422,3242],[20,34,55,100,33,422],[23,12,14,26,203,233]];
const maximum = Math.max(...matrix.flat())
console.log(maximum);

matrix.flat() will flatten the two-dimensional array into one dimension, ... syntax will put each value from the one-dimensional array as its own argument to Math.max, which then finds the biggest one.

about 4 years ago · Juan Pablo Isaza Denunciar

0

There is one small mistake. When you iterate the column make sure you iterate the number of columns.

matrix.length gives you the number of rows and matrix[i].length gives you the number of columns.

const matrix = [[2,56,10,14,422,3242],[20,34,55,100,33,422],[23,12,14,26,203,233]];
let maximum = matrix[0][0];
for(var row = 0; row < matrix.length; row++){
  for(var col = 0; col < matrix[row].length; col++){
    if(matrix[row][col] > maximum){
      maximum = matrix[row][col];
    };
  };  
};
document.write(' -- ', maximum);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You are taking matrix.length for no. of column as well, but it gives you no. of rows i.e. 3 but in your case but no. of column is 6 . that's why it only check for 3 numbers


const matrix = [[2,56,10,14,422,3242],[20,34,55,100,33,422],[23,12,14,26,203,233]];
let maximum = matrix[0][0];
for(var row = 0; row < matrix.length; row++){
  for(var col = 0; col < matrix[0].length; col++){          <--- Correction
    if(matrix[row][col] > maximum){
      maximum = matrix[row][col];
    };
  };  
};
document.write(' -- ', maximum);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda