In JS if you would like to split user entry into an array what is the best way of going about it?
For example:
entry = prompt("Enter your name") for (i=0; i<entry.length; i++) { entryArray[i] = entry.charAt([i]); } // entryArray=['j', 'e', 'a', 'n', 's', 'y'] after loop
Perhaps I'm going about this the wrong way - would appreciate any help!
Use the .split()
method. When specifying an empty string as the separator, the split()
method will return an array with one element per character.
entry = prompt("Enter your name") entryArray = entry.split("");
const array = [...entry]; // entry="i am" => array=["i"," ","a","m"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With