I'm new to Javascript and am taking a class to learn more. Part of my homework was to make a button, that on click prompts you for how many cookies you want. Then gives you a total. The per cookie price is conditional on the amount of cookies bought.
This is what I have. When I try debuggers I'm told my else statement on line 25 in undefined.
So basically it loops through and then gives Total: $NaN and doesn't stop the loop.
Any help would be appreciated.
<script>
function buyCookies()
{
var numCookiesP = document.getElementById("numCookies");
numCookiesP.innerHTML = parseInt (prompt("How many cookies would you like to buy?"));
if(numCookies <= 5)
{
document.write("Total: $" + (numCookies * 1.25)).toFixed(2);
}
else if(numCookies <= 11)
{
document.write("Total: $" + (numCookies * 1.15)).toFixed(2);
}
else
{
document.write("Total: $" + (numCookies * 1.00)).toFixed(2);
}
}
</script>
</head>
<body>
<input type="button" value="Cookies" onclick="buyCookies()">
<div>
<p id="numCookies"</p>
</div>
numCookies
variable, not tonumCookiesP.innerHTML
? – Bergi Apr 29 '13 at 21:57