+ 1
why is undefined written here
var a="w3resource"; for(var i=a.length;i>=0;i--){ document.write(a[i]); } why is it printing undefined along with the word?
5 Respostas
+ 4
Hi Ali Zain
Try this
var a="w3resource";
for(var i=a.length-1;i>-1;--i){
document.write(a[i]);
}
+ 2
The last index of a is not a.length, but a.length-1. a[i] doesn't exist thus undefined.
+ 2
var a="w3resource";
for(var i=a.length-1;i>=0;i--){
document.write(a[i]);
}
// I am just added a.length -1.
1.Now try this above code.
2. a.length is 10 but array index starts from 0 to 9.
3.first a[10] is going to print undefined.That's all.
0
Because we can't run this here ..
Give the code please so we can run it
0
Got it! Thanks alot