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

310
Views
Javascript - I have a problem using toFixed() in (if else)

I am new to javascript. I'm trying to code a simple program which has 2 variables, each one contains an average number of some calculations, and using if else it should print the variable which contains the higher average as the winner.

without using toFixed() there is no problem, the higher variable is the winner and its printed out, but when I use toFixed(), it prints the lower variable, not the higher one. why is that? picture of the problem

here is the code:

const avgDolphins = ((96 + 108 + 89) / 3).toFixed(2);
const avgKoalas = ((88 + 91 + 911) / 3).toFixed(2);
console.log(`dolphins = ${avgDolphins} \nkoalas = ${avgKoalas}`)

if (avgDolphins > avgKoalas) {
    console.log(`Team Dolphins WON! with ${avgDolphins} Average score!`);
} else if (avgKoalas > avgDolphins) {
    console.log(`Team Koalas WON! with ${avgKoalas} Average score!`);
} else {
    console.log(`DRAW! Both teams got the same average score which is ${avgKoalas}`);
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Both .toPrecision(2) (Reference) and .toFixed(2) (Reference) will return a string. You can use a parseFloat arount your calculation to fix this.

So the resulting code will look like this:

const avgDolphins = parseFloat(((96 + 108 + 89) / 3).toFixed(2));
const avgKoalas = parseFloat(((88 + 91 + 911) / 3).toFixed(2));
console.log(`dolphins = ${avgDolphins} \nkoalas = ${avgKoalas}`)

if (avgDolphins > avgKoalas) {
    console.log(`Team Dolphins WON! with ${avgDolphins} Average score!`);
} else if (avgKoalas > avgDolphins) {
    console.log(`Team Koalas WON! with ${avgKoalas} Average score!`);
} else {
    console.log(`DRAW! Both teams got the same average score which is ${avgKoalas}`);
}
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!