Hot Java
Hot Java

Reputation: 407

while loops with arrays

I'm attempting to have when the function is called that values in the array are displayed using while loops. It will display the first object except for the picture and not the other two

function products(){
    var txt=""
    var appliance = 0
var products = new Array();
products[0] = {name: "refrigerator" , price:88.99, 

    img:"img/refrigerator.jpg"};
products[1] = {name: "microwave oven" , price: 76.99 , img:"img/microwave.png"};
products[2] = {name: "dishwasher" , price:276.67 , img:"img/dishwasher.jpg"};
    /*var appliance = products[x].name + " " + products[x].price + " '" + products[x].img + "'>"*/
    while(appliance < 3){
        txt +=products[appliance].name + ' ' + products[appliance].price +" <img src='"+ products[appliance].img;"'>";
        appliance++;
    }
    document.getElementById("answer").innerHTML = txt
    }
    }

Upvotes: 0

Views: 65

Answers (3)

Hot Java
Hot Java

Reputation: 407

function products(){
    var txt=""
    var appliance = 0
var products = new Array();
products[0] = {name: "refrigerator" , price:88.99, img:"img/refrigerator.jpg"};
products[1] = {name: "microwave oven" , price: 76.99 , img:"img/microwave.jpg"};
products[2] = {name: "dishwasher" , price:276.67 , img:"img/dishwasher.jpg"};

while(appliance < 3){
    txt +=products[appliance].name + ' ' + products[appliance].price +" <img src='"+ products[appliance].img + "'>" +" <br />";
    appliance++;
}
document.getElementById("answer").innerHTML = txt
}
}

solving my own code like a boss

Upvotes: 0

ch271828n
ch271828n

Reputation: 17597

txt += products[ appliance ].name + " " + products[ appliance ].price + " '" + products[ appliance ].img + "'>" +" <br />";

Upvotes: 0

kockburn
kockburn

Reputation: 17616

products[appliance].name + ' ' + products[appliance].price + products[appliance].img;

Otherwise you are trying to show an array.

Notice you forgot the > in :

<img src=img/refrigerator.jpg

Upvotes: 1

Related Questions