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

164
Views
Javascript beginner programming quesion

This is my coding.

switch(education){
    case "no highschool diploma":
        salary="25636";
        break;
    case "a high school diploma":
        salary="35256";
        break;
    case "an Associate's degree":
        salary = "41496";
        break;
    case "an Bachelor's degree":
        salary = "59124";
        break;
    case "an Master's degree":
        salary = "69732";
        break;
    case "an Professional degree":
        salary = "89960";
        break;
    case "an Doctoral degree":
        salary = "84396";
        break;
}
console.log("In 2015, a person with "+education+" earned an average of "+ salary.toLocaleString("en-US") +"/year.");

I wanna know why the result is

In 2015, a person with a high school diploma earned an average of 35256/year.

Instead of

In 2015, a person with a high school diploma earned an average of 35,256/year.

Where am I wrong?

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

0

Your values are strings, make them numbers:

const stringVariable = "12345";
const numberVariable = 12345;

console.log(stringVariable.toLocaleString("en-US")); // Logs 12345
console.log(numberVariable.toLocaleString("en-US")); // Logs 12,345
about 4 years ago · Juan Pablo Isaza Report

0

you are working with String type, you need to convert to number first. console.log("In 2015, a person with "+education+" earned an average of "+ Number(salary).toLocaleString("en-US") +"/year.");

about 4 years ago · Juan Pablo Isaza Report

0

Different data types ( e.g. Number, String, Date ) often have their own implementation of .toLocaleString()

Because you call .toLocaleString() on a String, you are currently using the default implementation as defined in the Object data type, which isn't being overwritten.

However if you want to use the version associated with a Number. You can cast a String to a Number using Number() before you call .toLocaleString().

If you want to learn more read: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toLocaleString

demo

const education = "a high school diploma";

switch(education){
    case "no highschool diploma":
        salary="25636";
        break;
    case "a high school diploma":
        salary="35256";
        break;
    case "an Associate's degree":
        salary = "41496";
        break;
    case "an Bachelor's degree":
        salary = "59124";
        break;
    case "an Master's degree":
        salary = "69732";
        break;
    case "an Professional degree":
        salary = "89960";
        break;
    case "an Doctoral degree":
        salary = "84396";
        break;
}
console.log("In 2015, a person with "+education+" earned an average of " + Number(salary).toLocaleString("en-US") +"/year.");

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!