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

84
Views
Javascript condition not accurate to me

I'm trying to get the bigger score to print, it works for numbers until 10, however for a reason unclear to me numbers above 10 do not work as expected, with for example firstScore being 10 and secondScore 5, yet it would print 5 instead of 10.

var firstScore = prompt('First exam score?');
var secondScore = prompt('Second exam score?');

if (firstScore > secondScore) {
  console.log(firstScore);
} else if (secondScore > firstScore) {
  console.log(secondScore);
} else {
  console.log('Wrong parameter');
}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

As the comment says, the return type is string but according to you, you want that to be int to perform mathematical operations. Simple solution will be to parse the input to int

var firstScore = parseInt(prompt('First exam score?'))
var secondScore = parseInt(prompt('Second exam score?'))

if (firstScore > secondScore) {
    console.log(firstScore);
} else if (secondScore > firstScore) {
    console.log(secondScore);
} else {
    console.log('Wrong parameter');
}
about 4 years ago · Juan Pablo Isaza Report

0

The thing is you are trying to compare two strings, try parsing the result get from your prompt to integer or float with parseInt() like this :

var firstScore = parseInt(prompt('First exam score?'));
var secondScore = parseInt(prompt('Second exam score?'));

if (firstScore > secondScore) {
    console.log(firstScore);
} else if (secondScore > firstScore) {
    console.log(secondScore);
} else {
    console.log('Wrong parameter');
}
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!