function enterBook()
{
var myBooks = new Array();
var myPrices = new Array();
var total = 0;
for (i = 1; i <= 5; i++)
{
myBooks[i] = prompt("Enter title of book","");
myPrices[i] = parseFloat(prompt("Enter price of book",""));
document.getElementById("output").innerHTML += i + ". " + myBooks[i] + ". Price: £" + myPrices[i] + "<br />";
}
for (var i in myPrices)
{
total += myPrices[i];
document.getElementById("total").innerHTML = total;
}
}
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.
Finally, 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:
Code:
function enterBook()
{
var total = 0;
for (var i = 1; i <= 5; i++)
{
var title = prompt("Enter title of book","");
var price = parseFloat(prompt("Enter price of book",""));
document.getElementById("output").innerHTML += i + ". " + title + ". Price: £" + price + "<br />";
total += price;
}
document.getElementById("total").innerHTML = total;
}
You do not want to use a for-in loop to iterate over an Array object. A for-in loop serves a very different purpose: It iterates over all the properties in an object. You want to use a regular loop when iterating over an Array.
Code:
var obj = {
a: "foo",
b: 123
};
for (var key in obj) {
if (obj.hasOwnProperty(key)) { // filter out properties that do not exist on "obj" but exist in its prototype chain
alert(key + "=" + obj[key]);
}
}
var arr = ["foo", 123];
for (var i = 0, length = arr.length; i < length; i++) {
alert("arr[" + i + "]=" + arr[i]);
}
Just to add to the previous comments:
(1) For-In loops are slower than the "normal" for loops
(2) A javascript array is an object, and it has various properties that are functions. If you have:
Code:
var a = []; //create empty array
var i;
for(i in a){ //display all properties
alert(i);
}
You will see a lot of stuff that aren't numerical values at all. If you try adding them to
"total" you will get a very bizarre result that you don't want at all.
Just to add to the previous comments:
(1) For-In loops are slower than the "normal" for loops
(2) A javascript array is an object, and it has various properties that are functions. If you have:
Code:
var a = []; //create empty array
var i;
for(i in a){ //display all properties
alert(i);
}
You will see a lot of stuff that aren't numerical values at all. If you try adding them to
"total" you will get a very bizarre result that you don't want at all.
Executing the above code ONLY will display nothing.
Modifying to show both key and value assignments will at least show something.
Code:
<script type="text/javascript">
var a = []; //create empty array
a['a'] = 1;
a['b'] = 2;
a['c'] = 3;
var i;
var sum = 0;
for(i in a){ //display all properties
alert(i+' '+a[i]);
sum += a[i];
}
alert('Sum: '+sum);
</script>
Note: If you have any methods attached to the 'a' array, they would show as well,
which would not be what you might expect for the summation portion of your question.
Bookmarks