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 3 of 3

Thread: loop trouble

  1. #1
    Gez
    Gez is offline
    New Coder
    Join Date
    May 2008
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    loop trouble

    Hey, i can't understand why this loop isn't working? I want it to go through all entries in the array until it finds the one with the isbn which has been entered by the user and save the values to variables, i think it might be something to do with the if statement.

    Code:
    // define the catalogue
    var catalogue= new Array();
    catalogue[0]= new Array(3765834619,"Neuro-linguistic Programming for Dummies",12.99,950);
    catalogue[1]= new Array(5673423435,"Excel 2007 VBA Programming for Dummies",11.99,900);
    catalogue[2]= new Array(7454982674,"The Forgotten Garden",9.99,450);
    catalogue[3]= new Array(8356583865,"The Road Home",9.99,500);
    catalogue[4]= new Array(1629546824,"No Time For Goodbye'",10.99,600);
    catalogue[5]= new Array(3547975624,"The Outcast",10.99,670);
    catalogue[6]= new Array(1348795463,"How to Write Songs on Guitar",15.99,1010);
    catalogue[7]= new Array(5673519472,"Fretboard Roadmaps",12.992,2700),
    catalogue[8]= new Array(2730471465,"Management and Organisational Behaviour",20.99,1000);
    catalogue[9]= new Array(4859716360,"Leadership Coaching",18.99,900);
    
    var isbn;
    var quantity;
    var order = new Array();
    var weight;
    var price;
    var title;
    
    // function to get the order from the user
    function get_order (){
    
    // get the isbn number + quantity
    isbn = prompt("Please enter the ISBN number of the book you wish to order:");
    isbn = parseInt(isbn);
    quantity = prompt("How many would you like to buy?:");
    quantity = parseInt(quantity);
    
    // alert(isbn);
    // alert(quantity);
    
    // assign values to the orders array
    order [0]= new Array ( isbn, quantity );
    
    // alert (order[0][0]);
    // alert (order[0][1]);
    
    }
    
    // function to get the details of the order
    function order_details(catalogue, isbn){
    
    
    for (i = 0 ; i < catalogue.length ; i++){
    
    // alert (catalogue[i][0]);
    
    var isbnspec = parseInt(catalogue[i][0]);
    
    //alert (isbnspec);
    
    //alert (isbn);
    
    if ( isbn == isbnspec ) {
    
    alert ("I'm here!" + isbnspec);
    
    weight = catalogue[i][3];
    price = catalogue[i][2];
    title = catalogue[i][1];
    
    alert (weight);
    alert (price);
    alert (title);
    
    }
    
    // alert(i);
    
    }
    }
    
    
    get_order();
    
    order_details(catalogue);
    
    // alert("Order:" + '\n' + "Hello!");
    
    
    // alert(isbn);
    // alert(quantity);
    Thanks
    Gez

  • #2
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    It's because you are saying that isbn is a variable that has to be passed in.
    Code:
    function order_details(catalogue, isbn){
    This means that the isbn variable in your function is passed in through the function parameters and it doesn't pull in the variable isbn

  • #3
    Gez
    Gez is offline
    New Coder
    Join Date
    May 2008
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts
    ah thats got it! thanks!
    Gez


  •  

    Posting Permissions

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