Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    List items of array using a loop - help!

    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
    Last edited by Bullers; 08-05-2008 at 09:42 PM.

  • #2
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    Your arrays will need to be set up differently I think something like:
    Code:
    //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:
    Code:
    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>");
    }

  • #3
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm, that didn't work for me :S Thank you though for your help, do you have any other ideas?

  • #4
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    This got the first one working
    Code:
    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>");
    }

  • #5
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    You should start with appropriate data structures for your application (and strings are bad at holding more than one information):
    Code:
    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
    Last edited by dumpfi; 08-06-2008 at 04:04 PM. Reason: Replaced = with == in BookStore.getBook()
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #6
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #7
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    I believe that the 'this' keyword in dumpfi's code is causing problems by keeping the variables confined within the function

  • #8
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    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
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #9
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That works perfectly, thanks! I'm just wondering, is there a simpler way of doing it?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •