...

View Full Version : using a counter as a variable name



Gez
08-10-2008, 07:15 PM
Is it possible to use the counter in a FOR loop as part of a variable name that is declared within the loop?

i.e.


for (x=0; x<10; x++){

var hello(**counter goes here**) = 12345
var hello0 = 12345

}

Thanks
Gez

ninnypants
08-10-2008, 07:38 PM
You can't do that the main way that counters are used with variables in for loops is

for(x=0; x<10; x++){
array[x] = 12345;
}

Philip M
08-10-2008, 07:48 PM
Is this what you mean?


<script type = "text/javascript">
var hello = new Array();
for (x = 0; x < 10; x++) {
hello[x] = 12345 * x;
}
alert (hello[9])
</script>


When the questions are being asked, that's when you come up with the answers. - Sports commenator.

Gez
08-10-2008, 08:09 PM
well i have an array called 'orders' which has the isbn numbers of books that the users enters, then a loop inside this loop to look at the catalogue array and create a set of variables for each order in the orders array, but im not quite sure i can do this if i can't name the variables from the counter, this is what i've go so far:


// function to get the order from the user
function get_order (){

// see how many books they want overall
bookamount = prompt("How many books would you like to order altogether?:");

for ( c = 0; c < bookamount ; c++){

// get the isbn number + quantity
isbn = prompt("Please enter the ISBN number of the book you wish to order(10-digit code):");
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 [c]= new Array ( isbn, quantity );

// alert (order[c][0]);
// alert (order[c][1]);

}

}

// function to get the details of the order
function order_details(){
// loop that goes through the order array
for ( x=0 ; x < order.length ; x++){
// loop that locates the books details
for (i = 0 ; i < catalogue.length ; i++){

// alert (catalogue[i][0]);

var isbnspec = parseInt(catalogue[i][0]);

//alert (isbnspec);

//alert (isbn);

if ( isbn == isbnspec ) {

weight = catalogue[i][3];
price = catalogue[i][2];
title = catalogue[i][1];


//alert (weight);
//alert (price);
//alert (title);
}
}

// alert(i);

}

//alert (x);

}

dumpfi
08-10-2008, 10:03 PM
// function to get the order from the user
function get_order (){
// see how many books they want overall
bookamount = prompt("How many books would you like to order altogether?:");

for (var c = 0; c < bookamount ; c++){

// get the isbn number + quantity
isbn = prompt("Please enter the ISBN number of the book you wish to order(10-digit code):");
isbn = parseInt(isbn);
var entry = order_details(isbn);
if(entry == false) { // no entry found
alert("Invalid ISBN specified.");
--c; // decrement loop counter so that the user can re-enter the isbn number
}
else {
quantity = prompt("How many would you like to buy?:");
quantity = parseInt(quantity);
// assign values to the orders array
order [c]= [quantity].concat(entry); // append the details onto the end, i.e isbn is index 1, title is index 2, price is index 3 etc.
}
}

}

// function to get the details of the order
function order_details(isbn){
// outer loop removed
// loop that locates the books details
for (var i = 0 ; i < catalogue.length ; i++){
if(isbn == catalogue[i][0]) {
return catalogue[i]; // return catalogue info
}
//assignments removed
}
// when we get here, there was no catalogue entry with the isbn
return false; // return failure
}
dumpfi



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum