...

View Full Version : array output in a alert



Gez
08-11-2008, 05:09 PM
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

Philip M
08-11-2008, 05:54 PM
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.

dumpfi
08-11-2008, 06:26 PM
If you want to display the whole array content separated by a common delimiter you can also use the join() method, e.g.:
var na = new Array("one", "two", "three", "four", "five", "six");
alert ("The values are \n" + na.join("\n") + "\nSome more text");dumpfi

Gez
08-12-2008, 12:09 AM
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:



<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

rangana
08-12-2008, 03:37 AM
Your display_order function causes the mess. Try this instead:


function display_order(){

showorder = "Order Details: " + '\n' + '\n' + "Title :";
for (z = 0 ; z < orderdetails.length ; z++){
document.write(orderdetails[z][1]);
}
+ "dfdsf";

alert(showorder);


}

Gez
08-13-2008, 05:27 PM
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

ninnypants
08-13-2008, 05:50 PM
Use what rangana did but instead of using 'document.write' build a text variable

Gez
08-13-2008, 06:00 PM
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)


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

Trinithis
08-13-2008, 06:16 PM
for (z = 0 ; z < orderdetails.length ; z++)
showorder += orderdetails[z][1];

Gez
08-13-2008, 06:29 PM
Thank you!!! that got it!!

Gez



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum