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 5 of 5
  1. #1
    Gez
    Gez is offline
    New Coder
    Join Date
    May 2008
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts

    using a counter as a variable name

    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.

    Code:
     for (x=0; x<10; x++){
    
           var hello(**counter goes here**) = 12345
           var hello0 = 12345
    
    }
    Thanks
    Gez

  • #2
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    You can't do that the main way that counters are used with variables in for loops is
    Code:
    for(x=0; x<10; x++){
         array[x] = 12345;
    }

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Is this what you mean?

    Code:
    <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.

  • #4
    Gez
    Gez is offline
    New Coder
    Join Date
    May 2008
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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:

    Code:
    // 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);
    	
    }

  • #5
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Code:
    // 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
    Last edited by dumpfi; 08-10-2008 at 10:07 PM.
    "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.


  •  

    Posting Permissions

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