Javascript Array structure


#1

var temp = prompt("How many countries have you been to?");
var input = [];
do{
var countryname = prompt("so list country no "+ temp); input.push(countryname);
temp--;

}while(temp);

    for(i = 0; i < input.length; i++){
    console.log(countryname[i]);
    }

I made a simple JS code to test my do/while loop and for loop. The premise of the code is to ask the user for "The number of countries he/she has been to" and then have the list the "name of the countries he/she has been to". Finally the snippet prints out the name of the countries in the for loop.

I though I had it covered, But when it prints out the names of the countries. it seems to print out the individual characters of the last entered country name rather than the entire name of the countries. One by one. What have I done wrong ? Stumped. Thank you.


#2

here:

console.log(countryname[i]);

this should be input[i], you store the countries in input. countryname simply contains the entry from the last prompt


#3

Ahhh.. I want to swear at myself. &(*&%&% .. How could I have missed that ? Thank you :slight_smile:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.