View Full Version : loop trouble

08-09-2008, 08:03 PM
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.

// 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);




// alert("Order:" + '\n' + "Hello!");

// alert(isbn);
// alert(quantity);


08-09-2008, 10:59 PM
It's because you are saying that isbn is a variable that has to be passed in.

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

08-10-2008, 02:17 AM
ah thats got it! thanks!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum