Comma-separated letters | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Comma-separated letters

https://code.sololearn.com/WmQIGvqjntDv/?ref=app This code above surpose to print all the letters contained in an array, but it prints each letter separed by a comma. Please, what was my mistake?

9th May 2017, 4:59 PM
Silas Junior
Silas Junior - avatar
3 Antworten
+ 6
you can use a string instead the array (with the corrections that MR Programmer suggested to avoid going above the string length) var name = "JAVA";
9th May 2017, 5:56 PM
Burey
Burey - avatar
+ 7
You have comma between each letter, because you use a special variable name ^^ As you must know, the global scope in JS is strored as properties of 'window' object... That's why you can use window.setInterval() without the 'window' reference. So a variable declared in the global scope ( as function declaration ), are properties of 'window' object: var v = 42; // is equivalent to: window.v = 42 Unfortunaly, your 'name' variable is in conflict with the window.name built-in property: https://developer.mozilla.org/fr/docs/Web/API/Window/name Such property store string, and expect string at assignation, so your array is implicitly turned to the default string representation of your array ( from the Array.toString() method, as if you document.write(name) ), so 'name' contain a 7 character long string: "J,A,V,A" You can fix it just by renaming your array ( and including the changes suggested by @MR Programmer ): var myname = new Array("J", "A", "V", "A"); var i = 0; var clk; function print() { if(myname.length < i-1) clearInterval(clk); document.write(myname[i]); i++; } clk = setInterval(print, 800);
10th May 2017, 7:35 AM
visph
visph - avatar
+ 2
after the i is equals to the text size clear the interval. var clk; function print() { if(name.length < i-1) clearInterval(clk); document.write(name[i]); i++; } clk = setInterval(print, 800);
9th May 2017, 5:07 PM
MR Programmer
MR Programmer - avatar