Reputation: 17
I'm trying to pick up javascript, and I'm writing a fairly simple script where a user gives a number of pennies and a number of days. The script will then calculate how much money the user would have if the number of pennies they received on every day doubled.
The problem I'm running into is that when I add a "for" loop into the script, I start to get a "Uncaught SyntaxError: Unexpected token )" error. As far as I can tell, all the syntax looks correct to me, and the error goes away if I change the loop to a "while" loop.
I'm curious about what exactly it is that I'm running into here, and how do I keep it from happening?
<html>
<head>
<script>
var penny = 0;
var days = 0;
var total = 0;
function pCalc() {
//establish variables
var penny = prompt("Enter number of pennies.", "Enter a number");
var days = prompt("Enter number of days.", "Enter a number");
var total = 0;
//double pennies on every day, and add each day's pennies to the total
for (i = 0, i < days, i++){
//add pennies to total
total = penny + total;
//double pennies for each day
penny = penny + penny;
}
}
</script>
</head>
<body>
<div style="margin: 20px; padding: 100px; width: 100px; height: 100px; background-color: #00f;" onClick="pCalc()">
<p style="font-weight:bold; font-size: 20px;">Click me</p>
</div>
</body>
</html>
Upvotes: 1
Views: 2801
Reputation: 1378
For loops are written like this:
for (i = 0; i < days; i++){
with semicolons. You used commas.
Upvotes: 5