Hi folks. Here’s the code I have so far:
[code]<script language=javascript type=text/javascript>
var i, sum=0;
for (i=1;i<=5;i++)
{
sum = sum+i;
document.write(“The total is: ” +sum);
document.write(“<br>”);
}
</script>
What I now need to do is modify the above to allow the user to input a value. So basically that would be 1+2+3+4+5+n?
I made an attempt at it, but I just don’t know where to put n in the loop. I am very new to javascript.
Attempt:
[code]
<html>
<head>
<title>For Loop</title>
<script language=javascript type=text/javascript>
var i, sum=0;
var n =eval(document.myform.number.value);
for (i=1;i<=5;i++)
{
sum = sum+i;
document.write(“The total is: ” +sum);
document.write(“<br>”);
}
</script>
</head>
<body>
<form name=”myform” onSubmit=”calculate();”>
<h2>Enter a number</h2>
<p><label for=”number”>Input number here: </label><input type=”text” size=”10″ name=”number” id=”number”/> </p>
<input type=”submit” value=”Calculate”/>
</form>
</body>
</html>
Anyway thanks. ?