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

181
Vistas
Create Functions with Parameters Returns - Javascript

Im a bit confused or maybe just a lack of knowledge. Just doing my first steps. My question is: I need to test when a number pair (like 100, 5 let's say) will compare correctly as numbers and incorrectly as strings. How to fix it properly. I cant figure out the best way to test this last option. 1st test is Both numbers the same 2nd test is first number largest 3rd test is second number largest

I thought maybe I should say True to the first test. And say False when numbers as strings. ??

<script type="text/JavaScript">
        alert(largest(false,false));
        alert(largest(true,false));
        alert(largest(false,true));
        alert();
    </script>
function largest (firstNum, secondNum) {
    let largestNumber = largest;
        firstNum = parseInt(prompt('Enter the first number: '));
        secondNum = parseInt(prompt('Enter the second number: '));
        if (firstNum == secondNum) {    //3
            return false;   ///  maybe True?.. see my question
        } else {
            if (firstNum > secondNum) {     //1
                return largest = firstNum;
            } else if (firstNum < secondNum) {     //2
                return largest = secondNum;
            }
        if (isNaN(firstNum, secondNum)) {        /// false if strings?  
            return false;
        }
        }
    return largestNumber;
}
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your function is doing so many things as I can see It's returning true/false, largest number, etc.

I suggest you to clean up a bit your code like this!

function largest() {
    const firstNum = parseInt(prompt('Enter the first number: '));
    const secondNum = parseInt(prompt('Enter the second number: '));
    if (isNaN(firstNum) || isNaN(secondNum)) {
        alert('Invalid input!');
        return null;
        // return largest(); // This could work too
    }

    if (firstNum > secondNum) {
        return firstNum;
    }

    return secondNum;
}

First, we ask the user to input both numbers Then, we check if the values are valid, isNaN helps us with that, and if there is something incorrect, we show and alert and return

If the numbers are valid, we check

If firstNumber is larger than secondNumber, the largest one should be firstNumber

Otherwise, we return secondNumber (Last case)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your question isn't well formulated so I wasn't entirely sure, but I assumed you mean that the compare should only return true if it was based on a numerical comparison (i.e. any string input should return false

If that was your question you could try it like this:

const arr = [[100,5],["100",5],[100,"5"],["100", "5"]]

// return true if first value is bigger
function compare(v1, v2) {
    if (typeof(v1)==="string" || typeof(v2)==="string")
      return false;
    else
      return (v1 > v2);
}

// return array [boolean, boolean] for each of the input values
function acompare(v1, v2) {        
    if (typeof(v1)==="string" || typeof(v2)==="string")
      return [false, false, false];
    else
      return [(v1 > v2), (v1 < v2), (v1===v2)];
}

for (let a of arr) {
    console.log(compare(a[0],a[1]), acompare(a[0],a[1]))
}

//returns
//true, [true, false]
//false, [false, false]
//false, [false, false]
//false, [false, false]

see: https://jsfiddle.net/d2sv7xcb/5/

This way whenever either of the arguments is a string it returns false, if not it the returns the value of the comparison?

@Edit: Following your comment I have added the === to the function return value, that way you get back an array for the comparison?

[false, false, false] = no valid comparison
[true, false, false] = value 1 > value 2
[false, true, false] = value 1 < value 2
[false, false, true] = value 1 === value 2
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