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

168
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 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