Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda