JavaScript nested loop doesn't work -


i'm doing assignment using nested loop ask user input information of 4 guess , print out alert. problem prompt repeat once.

var myarray = new array(2); for(var i=0; i<2; i++){     for(var j=0; j<2; j++){         myarray[i][j] = prompt("enter guess first name.");         myarray[i][j] = prompt("enter guess last name.");         myarray[i][j] = prompt("enter guess phone.");         myarray[i][j] = prompt("enter guess address.");     } } alert("first person : " + myarray[0][0]); alert("second person : " + myarray[0][1]); alert("third person : " + myarray[1][0]); alert("fourth person : " + myarray[1][1]); 

try change this:

var myarray = new array(); for(var i=0; i<2; i++){     myarray[i]=[]; // <- you're missing     for(var j=0; j<2; j++){         myarray[i][j] = prompt("enter guess first name.");         myarray[i][j] = prompt("enter guess last name.");         myarray[i][j] = prompt("enter guess phone.");         myarray[i][j] = prompt("enter guess address.");     } } alert("first person : " + myarray[0][0]); alert("second person : " + myarray[0][1]); alert("third person : " + myarray[1][0]); alert("fourth person : " + myarray[1][1]); 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -