<script type="text/javascript">
function show(){
var word = document.getElementById("inp").value;
var letters = [];
for(i=0; i<=word.length; i++){
letters = word.substring(i,i+1);
}
document.getElementById("div").innerHTML = letters[0];
//setInterval();
}
</script>
</head>
<body>
<input type="text" id="inp" />
<button onclick="show()">Show</button>
<br>
<div id="div"></div>
</body>
The output of "letters[0]" is showing up as undefined. I want to create an array, where, let's say if the input was "hello", then, output would be like:
letters[0] = "h"
letters[1] = "e"
letters[2] = "l"
....
And also, I would like to add a setInterval() function to it, so that it displays the letters, one by one, with a delay.
Thanks!
i<=word.lengthcan’t be right. It’s supposed to bei<word.length, because it will go over the last index of the string. This doesn’t throw an error when usingsubstring, because it will simply return an empty string. Be aware of that.