I'm having trouble with my code because it's saying that "t1" is not defined, even though it's the name of my text box. I tried making the variable global by declaring it outside the function, but it didn't solve the issue. Interestingly, I had a similar problem with "t2" at first, but making it global fixed that error.
<head>
<script language="javascript">
var x=parseInt(t1.value);
function nn()
{
var i=1;
var s=0;
while (i<=x)
{
s=s+i;
i++;
}
}
t2.value=s;
</script>
</head>
<body>
Enter A Number :
<input type=text size=20 name="t1">
<input type=button onclick="nn()" value="Sum of Natural Numbers">
<br>
Sum Obtained :
<input type=text size=20 name="t2">
</body>
I need help understanding this as I'm new to coding. Any assistance would be greatly appreciated!