1

I have the following Javascript. I am trying to calculate the total of the myCosts array values using the 'for in; loop and then display them on the page. I think i am nearly there.

function enterFood() 
{ 
    var myFoods = new Array() 
    var myCosts = new Array() 

    for (i = 1; i <= 5; i++) 
    { 
        myFoods[i] = prompt("Enter name of Food",""); 
        myCosts[i] = parseFloat(prompt("Enter cost of food","")); 
        document.getElementById("cost").innerHTML += i + ". " + myFoods[i] + ". Cost: £" + myCosts[i] + "<br />"; 
    } 

    for (var i in myCosts) 
    { 
        document.getElementById("total_cost").innerHTML =+ i; 
    } 
}  

Can anybody offer any help so I can complete this?

2
  • sidenote: JavaScript “For …in” with Arrays Commented Feb 11, 2013 at 12:47
  • How is it not working for you? Expected results versus generated results. Commented Feb 19, 2013 at 17:45

2 Answers 2

2

You're missing a var keyword before the i in the for loop. Without it i will become a global variable. Also, using the array literal notation ([]) is shorter/faster/cleaner than writing new Array(), like so: myArray = []; Just a general tip... Also note that for (var ... in ...) is used to iterate through the members of an object and generally not to loop through an array.

If you're not using the values in the myBooks and myPrices arrays later in your script then you don't need any arrays here - you can just combine your two loops and save the values returned from the prompts in temporary variables inside the loop. See below:

function enterFood()
{
    var total_cost = 0;
    for (var i = 1; i <= 5; i++)
    { 
        var name = prompt("Enter name of Food","");
        var cost = parseFloat(prompt("Enter cost of food","")); 
        document.getElementById("cost").innerHTML += i + ". " + name + ". Cost: £" + cost + "<br />";
        total_cost += cost;
    }
    document.getElementById("total_cost").innerHTML = total_cost;
}

Additionally just looking at your code have a few comments:

1) Missing var keyword before the i inside the for loop. Without it i will become a GLOBAL variable.

2) In Javascript an array is an object, it has various properties that are functions. Using the array literal notation (i.e. [] ) is cleaner/shorter and faster than writing new Array..for example myArray = []

3) The For-In loop is used to iterate through the members of an object and generally NOT to loop through an array. For-In loops are slower than normal loops. You want to use a regular loop for iterating over an array.

4) If you are not using the values in the myBooks and myPrices arrays later on in your script then you do not need any arrays here, you could just combine your 2 loops and save the values returned from the prompts inside temporary variables inside the loop.

EDIT: As corrected by @Teemu, i is hoisted from for..in loop so will not become global.

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

3 Comments

Actually i won't become global, it's hoisted from for..in loop.
thanks @Teemu you are right. I have added an edit with your correction.
Oh, how I sometimes envy you, native English speakers! You can write really huge texts out of piece of nothing in a couple of minutes...
2

You should iterate array correctly:

var totalElement = document.getElementById("total_cost"),
    total_cost = parseFloat(totalElement.innerHTML);

for (var i = 0, len < myCosts.length; i < len; i++) {
    total_cost += myCosts[i];
}

totalElement.innerHTML = total_cost;

Comments

Your Answer

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