0

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
    }
    }
2
  • 1
    What are you intended to see? A certain property of the products object or the entire thing printed out? Commented Mar 15, 2015 at 0:57
  • The loop itself is fine (although it would be better to avoid the hard-coded values and the for form is often more idiomatic). Please write a title - and use tags - that apply to the problem. Commented Mar 15, 2015 at 0:59

3 Answers 3

1
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
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked.
0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.