array using a while loop to display in a bulleted list

I am very new to Javascript and am trying to recreate the following code using a While loop instead of a For loop. I can get it to list but not in an unordered list. Any help would be much appreciated.

var middle = "Wayne";
var streets = ["Elm Street", "Sesame Street", "Coronation Street", "Jump 
Street", "Wall Street"];
var sLen = streets.length;
var text = "<ul>";
for (i = 0; i < sLen; i++){
text += "<li>" + middle + " " + streets[i] + "</li>";
}
text += "</ul>";
document.getElementById("nameList").innerHTML = text;

2 answers

  • answered 2018-02-13 01:07 abeltre1

    var i = 0;
    while(i<sLen){
       ...execution code goes here...  
       i++;
    }
    

    Please review this reference to learn more about while loops while-loops-Javascript

  • answered 2018-02-13 01:40 wallah

    var middle = "Wayne";  
    var streets = ["Elm Street", "Sesame Street", "Coronation Street", "Jump 
    Street", "Wall Street"];
    var sLen = streets.length;
    var text = "<ul>";
    
    while(sLen--) {
        text += "<li>" + middle + " " + streets[sLen] + "</li>";
    }
    
    text += "</ul>";
    document.getElementById("nameList").innerHTML = text;
    

    I hope it will help you.