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

150
Vistas
What is missing to get correct output. I am trying to print Maximum and minimum in the array

When I run this code in browser console or any JavaScript environment, I get max and min as 1,1. Could someone help me to find the error.

var arr = [1,4,6,3];
function maxAndMin(arr) {
var max = arr[0];
var min = arr[0];
for(var i=0; i < arr.lenght; i++) {
  if(arr[i] > max) {
  max = arr[i];
}
  if(arr[i] < min) {
  min = arr[i];
}}
console.log("Max:",max,"Min:",min);
}  

 maxAndMin(arr);
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

arr.lenght has a typo, and is returning an undefined back. So you never iterate the for loop.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You had a typo with lenhgt. Also, I recommend starting your loop from 1, since the 0'th element is already processed at initialization time.

    var arr = [1,4,6,3];
    function maxAndMin(arr) {
    var max = arr[0];
    var min = arr[0];
    for(var i=1; i < arr.length; i++) {
      if(arr[i] > max) {
      max = arr[i];
    }
      if(arr[i] < min) {
      min = arr[i];
    }}
    console.log("Max:",max,"Min:",min);
    }  

     maxAndMin(arr);

about 4 years ago · Juan Pablo Isaza Denunciar

0

if you are using Javascript, then there is an alternate / easy way to find min and max of an array. Its just a one-liner

var arr = [1,4,6,3];

const min = arr.sort((a,b) => a-b)[0];
const max = arr.sort((a,b) => b-a)[0];

console.log('Original Array ==>', arr);
console.log('Min ==>', min);
console.log('Max ==>', max);

this way, you can reduce run time of your function without any for-loops and if-else loops.

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