What's the difference between using 'var' and 'parseFloat()' vs 'let' and 'Number(prompt' in practice exercise 22 & 23?

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 = 1000
0.1 + (income - 1000) * 0.3;
}
console.log(“tax is:”, tax );

2 Likes

Has anyone answered?

No not yet i kind of got the idea on my own but still am a little confused. My guess is parseFloat and parseInt are better to use for turning information into numbers, where as Number is better for just strings???

Yeah, that makes sense. Well, I hope we get an official answer. It would be nice to get more clarity.

1 Like

parseInt is for converting a non integer number to an int and parseFloat is for converting a non float (with out a decimal) to a float (with a decimal).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat

3 Likes