Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!