Reputation: 1839
I have an array of sentences that I wish to convert to unordered HTML lists each containing the words of a single sentence e.g [I play piano the can'] t
<ul>
<li> id = number</li>
<li>I</li>
<li>play</li>
<li>piano</li>
<li>the</li>
<li>can</li>
</ul>
I am using the following to ( I wish!!) iterate through the array to get the format that I want
function makeQuest() {
var quest=['I play piano the can', 'tired I am', 'are seven There week in a days'];
for (var i=0; i< quest.length; i++){
document.write('<ul class ="div3">')
document.write('<li id = "number">' + (i + 1) + '.' + ' '+ '</li>')
for (var j=0; j < quest[i].length; j++){
document.write('<li>')
document.write(quest[i][j])
document.write('</li>' + '</ul>')
}
}
};
makeQuest()
Instead I get using this script:
1.I
play piano the can
2. t
ired I am
3. a
re seven There week in a days.
What am I doing incorrectly?
Upvotes: 1
Views: 54
Reputation: 18619
split
the strings on spaces (your approach goes by characters instead of words):
function makeQuest() {
var quest=['I play piano the can', 'tired I am', 'are seven There week in a days'];
for (var i=0; i< quest.length; i++){
document.write('<ul class ="div3">')
document.write('<li>' + (i + 1) + '. </li>')
for (var j=0; j < quest[i].split(' ').length; j++){
document.write('<li>')
document.write(quest[i].split(' ')[j])
document.write('</li>')
}
document.write('</ul>')
}
};
makeQuest()
And (this is out of scope of your problem) don't use id="number"
more than once.
More about split()
Upvotes: 3