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

    array output in a alert

    so i have this multi dimentional array and i want to alert certain elements of the array in one alert box, i 'd also like to include some text in the alert as well, for example of the alert box output

    texttexttext
    value1
    value2
    value3
    texttexttext

    is this atall possible?

    thanks
    Gez
    Last edited by Gez; 08-11-2008 at 05:13 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Like this?


    <script type = "text/javascript">
    var na = new Array("one", "two", "three", "four", "five", "six")
    alert ("The values are \n" + na[1] + "\n" + na[4] + "\n" + "Some more text")
    </script>

    \n means newline.
    Last edited by Philip M; 08-11-2008 at 05:59 PM.

  • #3
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    If you want to display the whole array content separated by a common delimiter you can also use the join() method, e.g.:
    Code:
    var na = new Array("one", "two", "three", "four", "five", "six");
    alert ("The values are \n" + na.join("\n") + "\nSome more text");
    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.

  • #4
    Gez
    Gez is offline
    New Coder
    Join Date
    May 2008
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts
    ooo sorry guys i should've been more specific....ermm the thing is the elements of the array are user entered so i'd probly need a loop to go through and enter one each in turn?

    my code:

    Code:
    <script type="text/javascript">
    // 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);
    
    // declare variables used throughout the program
    	var isbn;
    	var bookamount;
    	var title;
    	var quantity;
    	var order = new Array();
    	var orderdetails = new Array();
    	var showorder;
    	var weight;
    	var totalweight;
    	var weightcost;
    	var price;
    	var totalbookcost;
    	var totalcost;
    	
    
    // 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("Quantity?:");
    		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 get_order_details(){
    // loop that goes through the order array
    	for ( x=0 ; x < order.length ; x++){
    
    	var x2 = x
    // loop that locates the books details
    		for (i=0 ; i < catalogue.length ; i++){
    
    	 // alert (catalogue[i][0]);
    
    		var isbnspecific = parseInt(catalogue[i][0]);
    
    	//alert (isbnspecific);
    
    		if ( order[x][0] == isbnspecific ) {
    
    // weight
    			weight = catalogue[i][3];
    // price
    			price = catalogue[i][2];
    // title
    			title = catalogue[i][1];
    // isbn
    			isbn = catalogue[i][0];
    			
    // assign the details to the array			
    			orderdetails[x]= new Array ( isbn, title, price, weight );
    			
    			//alert (orderdetails[x][3]);
    			//alert (orderdetails[x][2]);
    			//alert (orderdetails[x][1]);
    			//alert (orderdetails[x][0]);
    
    
    		}
    	//alert(i);
    		}
    	//alert (x);
    	}
    	
    	
    	
    }
    
    // function to calculate all the relevent costs
    	function work_out_costs(){
    
    // work out postage costs
    	totalweight = 0;
    
    	for ( q = 0 ; q < orderdetails.length ; q++){
    	
    	totalweight = totalweight + (parseInt(orderdetails[q][3]) * parseInt(order[q][1]));
    	
    	// alert (totalweight);
    	
    	}
    
    	// alert (totalweight);
    
    	if ( totalweight >= "1" && weight <= "500" )
    	
    		weightcost = "1";
    	
    	if ( totalweight >= "501" && weight <= "1000" )
    	
    		weightcost = "2";
    	
    	if ( totalweight >= "1001" && weight <= "2000" )
    	
    		weightcost = "3";
    	
    	if ( totalweight >= "2001" && weight <= "5000" )
    	
    		weightcost = "4";
    	
    	if ( totalweight >= "5001")
    	
    		weightcost = "5";
    	
    
    	 //alert (weightcost);
    
    // work out total book cost
    	totalbookcost = 0;
    
    	for ( w = 0 ; w < orderdetails.length ; w++ ){
    	
    	totalbookcost = totalbookcost + (parseFloat(orderdetails[w][2]) * parseFloat(order[w][1]));
    	
    	}
    
    
    // round the value to 2.d.p
    
    	totalbookcost = Math.round(totalbookcost*100)/100;
    
    	//alert (totalbookcost);
    
    // work out total costs
    	
    	totalcost = parseFloat(totalbookcost) + parseFloat(weightcost);
    
    // round the value to 2.d.p
    
    	totalcost = Math.round(totalcost*100)/100;
    
    	//alert (totalcost);
    
    }
    
    	function display_order(){
    
    	showorder = "Order Details: " + '\n' + '\n' +
    				 "Title :" + '\n' + 
    				 for (z = 0 ; z < orderdetails.length ; z++){ 
    				 document.write(orderdetails[z][1];
    				 } 
    				 + "dfdsf";
    
     	alert(
    	
    	showorder;
    		
    	);
    
    
    }
    
    	get_order();
    
    	get_order_details();
    
    	work_out_costs();
    
    	display_order();
    
    
    
    
    	// alert(isbn);
    	// alert(quantity);
    </script>
    and i kinda need it to display book by book from the 'orderdetails' array, e.g

    isbn1 title1 quantity1
    isbn2 title2 quantity2
    etc...

    any ideas?

    gez

  • #5
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Your display_order function causes the mess. Try this instead:
    Code:
    function display_order(){
    
    	showorder = "Order Details: " + '\n' + '\n' + "Title :";
    				 for (z = 0 ; z < orderdetails.length ; z++){ 
    				 document.write(orderdetails[z][1]);
    				 } 
    				 + "dfdsf";
    
     	alert(showorder);
    
    
    }
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #6
    Gez
    Gez is offline
    New Coder
    Join Date
    May 2008
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Rangana that works but i don't want it to document write (i know thats what was on my posted code), i need it to all show on one alert box, is this even possible? I'm still stuck on this so any suggestions apreciated!

    Gez

  • #7
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    Use what rangana did but instead of using 'document.write' build a text variable

  • #8
    Gez
    Gez is offline
    New Coder
    Join Date
    May 2008
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts
    sorry im not totally sure on what you mean by building a text variable only been doing javascript for alittle over a week, i always seem to get an error with the for loop.

    this is what i have at the moment (not working)

    Code:
    function display_order(){
    
    	showorder = "Order Details: " + '\n' + '\n' + "Title :";
    				 for (z = 0 ; z < orderdetails.length ; z++)
    				 + orderdetails[z][1] +
    				 
    				 "";
    
    	
    				
    
     	alert(showorder);
    
    
    }
    this code doesnt result in any error , just the results of the loop dont show up

    Gez
    Last edited by Gez; 08-13-2008 at 06:11 PM.

  • #9
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Code:
    				 for (z = 0 ; z < orderdetails.length ; z++)
    				 showorder += orderdetails[z][1];
    Trinithis

  • Users who have thanked Trinithis for this post:

    Gez (08-13-2008)

  • #10
    Gez
    Gez is offline
    New Coder
    Join Date
    May 2008
    Posts
    32
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you!!! that got it!!

    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
    •