0

esta funcion arroja el numero mayor y el menor de todos los ingresados por lo que quiero validar que el usuario si o si ingrese dos numeros pero no puedo conseguirlo

let arr = [-1, 5, 14, 0, 6];

const elevarNumeros = (arr = undefined) => {
    if (arr === undefined) return alert("Debes ingresar los numeros");
    if (!(arr instanceof Array)) return alert(`${arr} no es un número`);
    // Puse el if que sigue para tratar de validar pero no funciona
    if (length.arr < 1) return alert("Debes ingresar dos numeros o más");

    for (let num of arr) {
        if (typeof num !== "number") return alert(`${arr} no es un número`);
    }

    console.log(`El numero mas alto es el ${Math.max(...arr)} y el mas bajo ${Math.min(...arr)}`)
}

elevarNumeros(arr);
1
  • Las etiquetas se usan para indicar las tecnologías involucradas, por favor especifica qué lenguaje usas (javascript en este caso)
    – Benito-B
    el 8 dic. 2020 a las 21:34

3 respuestas 3

0

Ya tienes la respuesta a tu problema por parte de @nashvent
Como comentan es un error en la Sintaxis.

Eh querido dejarte un añadido en cuanto a posibles errores con diferentes tipos de datos específicos que pudiera llegar a insertar el usuario. Véase.

  • undefined
  • boolean
  • symbol
  • Infinity

let arr = [1, 2, 3, 4, -5]
const elevarNumeros = (arr = undefined) => {
    
    
    if (!(arr instanceof Array)) return alert(`${arr} No es un Array, es un ${(typeof arr)}.`);
 
    if (arr.length < 2) return alert("Debes ingresar dos numeros o más");

    for (let num of arr) {
        if (typeof num === "string") return alert(`${arr} no es un número`);
        // Si queires el "Infinity" puedes retirar la condicion para ello.
        if (typeof num !== 'number' || isNaN(num) || !isFinite(num)) return alert("Debes ingresar los numeros");
    }

    console.log(`El numero mas alto es el ${Math.max(...arr)} y el mas bajo ${Math.min(...arr)}`)
}

elevarNumeros(arr)

0

El error esta en el tercer if debido a que estas accediendo al tamaño del array de forma incorrecta y se validaria que tenga mas de 1 elemento.

 if (arr.length < 2) return alert("Debes ingresar dos numeros o más");

El código completo seria:

let arr = [-1];

const elevarNumeros = (arr = undefined) => {
    if (arr === undefined) return alert("Debes ingresar los numeros");
    if (!(arr instanceof Array)) return alert(`${arr} no es un número`);
    // Valida si existe menos 2 de elementos
    if (arr.length < 2) return alert("Debes ingresar dos numeros o más");

    for (let num of arr) {
        if (typeof num !== "number") return alert(`${arr} no es un número`);
    }

    console.log(`El numero mas alto es el ${Math.max(...arr)} y el mas bajo ${Math.min(...arr)}`)
}

elevarNumeros(arr);

0

intenta este codigo

if (arr.length >= 1) return alert("Debes ingresar dos numeros o más");

la etiqueta length va posterior a tu variable introducir el código aquí

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.