I watch a video where some man does the same thing and it works.
var beer = 2.50;
var hot_dog = 2.75;
var pop = 3.00;
var cash = 15.00;
function hot_dog() {
cash = cash - hot_dog;
console.log(cash)
}
hot_dog();
I watch a video where some man does the same thing and it works.
var beer = 2.50;
var hot_dog = 2.75;
var pop = 3.00;
var cash = 15.00;
function hot_dog() {
cash = cash - hot_dog;
console.log(cash)
}
hot_dog();
You have a variable named hot_dog
and tried to name a function
hot_dog
as well. You can't have it both ways. Use separate names and your code should work, e.g.:
var beer = 2.50;
var hot_dog = 2.75;
var pop = 3.00;
var cash = 15.00;
function hot_dog_func() {
cash = cash - hot_dog;
console.log(cash)
}
hot_dog_func();
cash = cash - pop;
console.log(cash)
Variables and functions must be unique in terms of names. hot_dog
is a variable. It's also declared as a function.
You can re-frame this by making the function describe what it does instead of giving it a random name:
var beer = 2.50;
var hot_dog = 2.75;
var pop = 3.00;
var cash = 15.00;
function buy(item_cost) {
cash = cash - item_cost;
console.log(cash)
}
buy(hot_dog);
cash = cash - pop;
console.log(cash)
In actual JavaScript code you might want to make a class
that defines a cash balance and so on.
Please refer to this post: What happens when JavaScript variable name and function name is the same?
When you are using hot_dog variable in the subtract operation which is not a number, thus anything subtracted from NaN is NaN