1
    <!doctype html>
<html lang="en">
<head>
    <title>Test</title>
    <meta charset="utf-8">

<script>
    var foodItem0 = prompt("Enter your favorite food");
    var foodItem1 = prompt("Enter your second favorite food");
    var foodItem2 = prompt("Enter your third favorite food");
    var allFood = [foodItem0,foodItem1,foodItem2];

    function favoritefood(){
        for(x=0;x>allFood.length;x++){
        var id="item"+x;
        var li=document.getElementById(id);
        li.innerHTML=allFood[x];
        }
    }
window.onload=favoritefood; 


</script>
</head>

<body>
    <h1>Top Three Favorite Food Items</h1>
    <ol>
    <li id="item0"></li>
    <li id="item1"></li>
    <li id="item3"></li>
    </ol>

</body>
</html>

If i am allowed to declare the array "allfood" like that please explain why the code is not working the list numbers are showing up but not the food that i enter in the prompts

2
  • Look in your browser console for errors. document.getElementById("item2") returns null. Your ids should be item0, item1, item2. Commented Dec 18, 2013 at 1:43
  • 1
    Also for(x=0;x < allFood.length;x++){. < not > Commented Dec 18, 2013 at 1:43

2 Answers 2

3
var allFood = [foodItem0,foodItem1,foodItem2];

this is perfectly valid. This means that you are creating an array.

  1. The code doesn't work, because of the condition in the for loop.

    for(x=0;x>allFood.length;x++){
    

    It should have been

    for(x=0; x < allFood.length;x++){
    
  2. After fixing that, you need to change the id of the last list item, which says item3, you might want to change it to item2.

Sign up to request clarification or add additional context in comments.

Comments

0

Your "less than" symbol in your for loop is backwards. As such, your loop is exiting without running the code inside.

1 Comment

thefoutheye got to it before me. He is correct. Upvoting his answer.

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.