-4

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();
Muzner
  • 1
  • 1
  • 1
    Your code does not run. `Uncaught TypeError: hot_dog is not a function` – CertainPerformance Jan 08 '19 at 00:51
  • 5
    You have declared a variable and a function with the same name: *hot_dog*. Only one can exist. Since function declarations are processed before variable declarations (and before any code is executed), the variable declaration wins due to the assignment `hot_dog = 2.75`. – RobG Jan 08 '19 at 00:52
  • Why do you have a float variable and a function both called hot_dog? – Tom G Jan 08 '19 at 00:52
  • Possible duplicate of [Function becomes undefined when declaring local variable with same name](https://stackoverflow.com/questions/40554209/function-becomes-undefined-when-declaring-local-variable-with-same-name) (See accepted answer) – faintsignal Jan 08 '19 at 00:58

3 Answers3

0

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)
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
0

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.

tadman
  • 208,517
  • 23
  • 234
  • 262
0

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