+ 1

How do I make this JavaScript code split words not letters

var text = "HI this is a text"; for(var i = 0; i < text.length; i++){ console.log(text[i]); } How do I make it to do words not letter https://code.sololearn.com/W8Gr25tIru8c/?ref=app

4th Sep 2022, 7:52 AM
heaker Gamer
2 Respostas
+ 4
// This way what you choose is possible but long // the code tests text on spaces and print parts between // or you can use i.e. a split function var text = "HI this is a text"; text = text.split(" "); for(var i = 0; i < text.length; i++){ console.log(text[i]); }
4th Sep 2022, 7:55 AM
JaScript
JaScript - avatar
+ 4
Currently you are iterating over the string and printing each character in console. To get words, you need to split the strings by a space " ", you can do that by using split(" ") method of strings. See the answer by JaScript
4th Sep 2022, 10:54 AM
Sandeep
Sandeep - avatar