I did not see any mention of parseFloat() or parseInt() in Chapter 2 of the book and didnt know if i should rather use parseFloat() than this other method i am using. Is it better to be using parseFloat() or parseInt() because i’ll be using it more in the future, or it doesn’t really matter? I guess i should look more what this Keyword means.
Here is some example of what i’m talking about.
Example1
// let y = 10
let x = Number(prompt(“Whats your age”));
if (x < 12 && x > 67 ){
console.log(alert(“You must pay " + y * .5 + " Dollars”));
} else {
console.log(alert(“You Must Pay " + y + " Dollars”));
}
vs Example1
// var price = 50;
var text = prompt(“What is your age?”);
var age = parseInt(text);
if (age < 12 || age > 67) {
console.log(You have to pay ${price * 0.5} Euro.
)
}
else {
console.log(You have to pay ${price} Euro.
)
}
Example2:
// let income = Number(prompt(“How Much Was Your Gross Salary For 2020?”));
if (income < 1000) {
var tax = income * .1
console.log(income);
} else {
var tax = 1000 * .1 + ( income - 1000) * .3;
}
console.log(“tax is:”, tax);
vs Example2
//
var input = prompt(“What is your income?”);
var income = parseFloat(input);
console.log(income);
if (income < 1000) {
var tax = income0.1;
}
else {
var tax = 10000.1 + (income - 1000) * 0.3;
}
console.log(“tax is:”, tax );