...

View Full Version : List items of array using a loop - help!



Bullers
08-05-2008, 10:38 PM
Hey guys im really new to javascript and I'm trying to learn I just need a bit of a head start with your help:

I have two arrays:


//Create Array with 3 books stored

var books = [];
books[0] = "1565923928:Programming In Javascript:22.90:950";
books[1] = "2010948473:Java The Definitive Guide:14.99:700";
books[2] = "1098767854:Javascript For Beginners:9.99:600";

//Create Array with 1 order stored

var order = [];
order[0] = "1565923928:3";
order[1] = "2010948473:10";
order[2] = "1098767854:1";

Where order[0] = "1565923928:3"
first number(1565923928) = ISBN and second number(3) = quantity

I need to produce a neat listing of the order e.g.


ISBN: 1565923928
Book Title: Programming In Javascript
Cost: £22.90
Weight: 950g
Quantity: 3

ISBN: 2010948473
Book Title: Java The Definitive Guide
Cost: £14.99
Weight: 700g
Quantity: 10

Total Cost: (number)


It can't be outputted on a document, it has to be outputted on like a echo box.

Thank you very much

ninnypants
08-05-2008, 11:29 PM
Your arrays will need to be set up differently I think something like:


//Create Array with 3 books stored

var books = [];
books[0] = "1565923928:Programming In javascript:22.90:950";
books[1] = "2010948473:Java The Definitive Guide:14.99:700";
books[2] = "1098767854:Javascript For Beginners:9.99:600";

//Create Array with 1 order stored

var order = [];
order[0]['item'] = books[0];
order[0]['item']['qty'] = 3;
order[1]['item'] = books[1]
order[1]['item']['qty'] = 10;
order[2] = "1098767854:1";
order[2]['item'] = books[2];
order[2]['item']['qty'] = 1;

With them structured like this you can write a loop to print them to the page:


for(i=0;i<order.length;i++){
item = order[i]['item'].split(':');
document.write("
<dl>
<dt>ISBN:</dt>
<dd>"+item[0]+"</dd>
<dt>Book:</dt>
<dd>"+item[1]+"</dd>
<dt>Cost:</dt>
<dd>"+item[2]+"</dd>
<dt>Weight:</dt>
<dd>"+item[3]+"</dd>
<dt>Quantity:</dt>
<dd>"+order[i]['item']['qty']+"</dd>
</dl>");
}

Bullers
08-06-2008, 12:01 AM
Hmm, that didn't work for me :S Thank you though for your help, do you have any other ideas?

ninnypants
08-06-2008, 12:32 AM
This got the first one working


var order = new Array();
order[0] = new Array();
order[0]['item'] = books[0];
order[0]['item']['qty'] = 3;

for(i=0;i<order.length;i++){
item = order[i]['item'].split(':');
document.write("<dl><dt>ISBN:</dt><dd>"+item[0]+"</dd><dt>Book:</dt><dd>"+item[1]+"</dd><dt>Cost:</dt><dd>"+item[2]+"</dd><dt>Weight:</dt><dd>"+item[3]+"</dd><dt>Quantity:</dt><dd>"+order[i]['item']['qty']+"</dd></dl>");
}

dumpfi
08-06-2008, 09:58 AM
You should start with appropriate data structures for your application (and strings are bad at holding more than one information):
function Book(isbn, title, price, weight) // prices are in cent to minimize rounding errors
{
this.isbn = isbn;
this.title = title;
this.price = price;
this.weight = weight;
}
Book.prototype.toString = function()
{
return 'ISBN: ' + this.isbn + '\n'
+ 'Book Title: ' + this.title + '\n'
+ 'Cost: &#163;' + (this.price / 100) + '\n'
+ 'Weight: ' + this.weight + 'g';
}

function Order(book, amount)
{
this.book = book;
this.amount = amount;
}
Order.prototype.toString = function()
{
return this.book.toString() + '\nQuantity: ' + this.amount;
}

function BookStore(books)
{
this.books = books;
}
BookStore.prototype = {
getBook : function(isbn)
{
for(var i = 0; i < this.books.length; ++i)
{
if(this.books[i].isbn == isbn)
{
return this.books[i];
}
}
return null; // no book found
}
};

function Orders(orders)
{
this.orders = orders;
}
Orders.prototype.toString = function()
{
var
str = '',
i = 0,
cost = 0;

for(; i < this.orders.length; ++i)
{
str += this.orders[i].toString() + '\n\n';
cost += this.orders[i].book.price * this.orders[i].amount;
}
return str + 'Total Cost: &#163;' + (cost / 100);
}
//Create Array with 3 books stored

var books = new BookStore(
[new Book('1565923928', 'Programming in Javascript', 2290, 950),
new Book('2010948473', 'Java The Definitive Guide', 1499, 700),
new Book('1098767854', 'Javascript for Beginners', 999, 600)]
);

//Create Array with 1 order stored

var orders = new Orders(
[new Order(books.getBook('1565923928'), 3),
new Order(books.getBook('2010948473'), 10),
new Order(books.getBook('1098767854'), 1)]
);

alert(orders.toString());dumpfi

Bullers
08-06-2008, 04:20 PM
It works perfectly apart from it does this:


ISBN: 1098767854
Book Title: Programming in Javascript
Cost: 22.9
Weight: 950g
Quantity: 3

ISBN: 1098767854
Book Title: Programming in Javascript
Cost: 22.9
Weight: 950g
Quantity: 10

ISBN: 1098767854
Book Title: Programming in Javascript
Cost: 22.9
Weight: 950g
Quantity: 1

Total Cost: 320.6

The title, isbn,cost and weight isnt changing? any ideas?

thank you!

ninnypants
08-06-2008, 04:30 PM
I believe that the 'this' keyword in dumpfi's code is causing problems by keeping the variables confined within the function

dumpfi
08-06-2008, 05:02 PM
I edited my previous post to correct the issue.The getBook method of BookStore had an assignment (=), where an equality check (==) should have been.

dumpfi

Bullers
08-13-2008, 09:40 PM
That works perfectly, thanks! I'm just wondering, is there a simpler way of doing it?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum