CodePen issues with variables

hi. I am just learning Javascript, and I wanted to test my skills with codepen. I created a short thing to put into the console. You have your money, which is the variable moneyAmount, the formula that relates your money to to a item of purchase, which in this case is the starter weapon (this is the variable for the weapon cost: startWeaponCost. The formula variable is this, moneyAmountVariable. I want my money amount to be 1,000, and the starter weapon to cost 900. the formula should subtract 900 from 1,000. I have a couple of console.log(s) to show in the console what this looks like. here is my code:

let startWeaponCost = 900;
let moneyAmount = 1000;
let moneyAmountFormula = moneyAmount - startWeaponCost;
let dollars = " dollars";


if (moneyAmountFormula = moneyAmount - startWeaponCost){
  console.log("Starting Weapon purchased");
}


  console.log("price is $" + startWeaponCost);


console.log("you have " + moneyAmountFormula - startWeaponCost + dollars + " left.");




and here is what shows in console:

“Starting Weapon purchased”

“price is $900”

“NaN dollars left.”

why does it say NaN dollars left and not 100 dollars left? Help! sorry to dump all of that on you

Hello,
The reason you are getting NaN is because you added a string to a number which results in a string then attempted to subtract a number from a string
Wrap the operation moneyAmountVariable - startWeaponCost in () to make it work properly

Ok thanks! Ill try that real quick.

Yep it worked! Thanks so much.

1 Like