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?
CodePudding user response:
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
CodePudding user response:
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.");
CodePudding user response:
This is because salary="35256" is passed as a string. Try changing to salary=35256 and it should work
CodePudding user response:
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.");
CodePudding user response:
use interger values not string values
and please code this way...
const edu_salary =
{ "no highschool diploma" : 25636
, "a high school diploma" : 35256
, "an Associate's degree" : 41496
, "an Bachelor's degree" : 59124
, "an Master's degree" : 69732
, "an Professional degree" : 89960
, "an Doctoral degree" : 84396
}
let education = "a high school diploma"
console.log(`In 2015, a person with ${education} earned an average of ${edu_salary[education].toLocaleString('en-US')}/year.`)
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}