Hello!
Please, what is wrong in this code, it does nto write alert(y) as sum gor in the loop, it just writes y=1 (as it was before hte lop…???)
<!DOCTYPE html>
<html>
<head>
<script>
function m(){
var a=document.getElementById("demo").value;
var array = a.split('');
var a2=array.length;
alert("Array length: "+ a2);
alert("Element with index four: " + array[4]);
var c=document.getElementsByName("marc")[0].value;
alert("Second input box: " + c);
var m1=parseInt(c,10);
alert("Second input box as parseInt: " + m1);
m2=m1+3;
alert("Second input box added three: " + m2);
var z=Number(array[0]);
var z1=z+1000;
alert("First element of the array, number method, when addad 1000: " + z1);
var y=parseInt(array[0]);
alert("First lement of the array, parseInt "+ y);
for(var i in a2)
{
var y=y+parseInt(array[i]);
alert("in the loop, y: "+ y);
}
alert("Y after loop, out of loop: " + y);
}
</script>
</head>
<body>
<form>
<input type="text" id="demo" value=""/><br>
<p>Write a letter</>
<input type="text" name="marc" value=""/>
<input type="button" value="click" onclick="m()"/>
</form>
</body>
</html>
Hello!
Thanks, changed that but still does nto work (indexed values are not summed)
<html>
<head>
<script>
function m(){
var a=document.getElementById("demo").value;
var arr = a.split('');
var a2=arr.length;
alert("Array length: "+ a2);
alert("Element with index four: " + arr[4]);
var c=document.getElementsByName("marc")[0].value;
alert("Second input box: " + c);
var m1=parseInt(c,10);
alert("Second input box as parseInt: " + m1);
m2=m1+3;
alert("Second input box added three: " + m2);
var z=Number(arr[0]);
var z1=z+1000;
alert("First element of the array, number method, when added 1000: " + z1);
var y=parseInt(arr[0]);
alert("First lement of the array, parseInt "+ y);
for(var i in a2)
{
var y=y+parseInt(arr[i]);
alert("in the loop, y: "+ y);
}
alert("Y after loop, out of loop: " + y);
}
</script>
</head>
<body>
<form>
<input type="text" id="demo" value=""/><br>
<p>Write a letter</>
<input type="text" name="marc" value=""/>
<input type="button" value="click" onclick="m()"/>
</form>
</body>
</html>
Don’t mean to be pedantic, but you can.
AFAIK, array (or Array) is not a reserved word, but it is better to avoid using the name of built-in objects, properties, and methods as variables.
Hahaha its all good, we actually use it all the time in our production code. Its a special case though, where we want out react components to tell us if we are passing in the wrong sort of stuff as props; ie:
There is no reference to Array in the JavaScript code you posted - you appear to be using array which is a completely different variable name as the first letter is different.
I believed we were talking about the word array with a lowercase a. As using Array for anything but what it already does would be silly, which was stated before by both myself and @James_Hibbard
@RyanReese you should never be worried about posting something. These forums are here for you to learn. Making mistakes is all part of learning. Better to make them here than in production.