...

View Full Version : Help With Array



janjan_376
08-19-2009, 06:36 PM
<html>
<head>
<script type="text/javascript">
<!---
var Jane=new Array ("Female","Red","Lipstick","28",","45789546");
var Mary=new Array ("Female","Orange","Lipstick","25",","45789547");
var Elle=new Array ("Female","Blue","Lipstick","21",","45789548");
document.write(Jane[0]);
document.write(Jane[1]);
document.write(Jane[2]);
document.write(Jane[3]);
document.write(Jane[4]);
//-->
</script>
<html>

Please help me with the above code. I would like to have a prompt so I can enter one of the three girls name (array) then it would display all the elements. For example:

Enter a name: Jane
the output would be :

Female
Red
Lipstick
28
45789546


Please help. Thank you very much.

ckeyrouz
08-19-2009, 06:58 PM
Try this:


<html>
<head>
<script type="text/javascript">

var Jane = new Array ("Female","Red","Lipstick","28","45789546");
var Mary = new Array ("Female","Orange","Lipstick","25","45789547");
var Elle = new Array ("Female","Blue","Lipstick","21","45789548");

function listValues()
{
var arrayName = "";
do
{
arrayName = getArrName();
}
while(arrayName == "-1");
document.write(eval(arrayName+"[0]") + "<br/>");
document.write(eval(arrayName+"[1]") + "<br/>");
document.write(eval(arrayName+"[2]") + "<br/>");
document.write(eval(arrayName+"[3]") + "<br/>");
document.write(eval(arrayName+"[4]"));
}

function getArrName()
{
var arrayName = window.prompt("Enter a name : ");
try
{
eval(arrayName);
}
catch(e)
{
arrayName = "-1";
alert("Name does not exist, please choose another one.");
}
return arrayName;
}
</script>
<body onload="listValues()"></body>
<html>

Philip M
08-19-2009, 07:39 PM
More or less the ckeyrouz's script with a few cosmetic improvements, including avoid document.write().


<html>
<head>

<script type="text/javascript">

var Jane=["Female","Red","Lipstick","28","", "45789546"];
var Mary=["Female","Orange","Lipstick","25","", "45789547"];
var Elle=["Female","Blue","Lipstick","21","","45789548"];

function showIt(which) {
var n = which.value;
// name to lower case with capitalised first letter
n = n.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});

try {
eval(n);
}
catch(e) {
alert ("The name " + n + " does not exist, please choose another one.");
document.getElementById("gname").value = "";
document.getElementById("gname").focus();
return false;
}

nv = eval(n);
var result = n + "<br>";
result = result + nv[0] + "<br>";
result = result + nv[1] + "<br>";
result = result + nv[2] + "<br>";
result = result + nv[3] + "<br>";
result = result + nv[4] + "<br>";
result = result + nv[5] + "<br>";

document.getElementById("details").innerHTML = result;

}

</script>

<body>

<input type = "text" name = "gname" id = "gname">
<input type ="button" value = "Get Details" onclick = "showIt(gname)">
<br><br>
<div id = details></div>

</body>
</html>

I interpreted that nameArray[4] is intentionally blank to allow for some future attribute to be included, e.g. bra size. :D

ckeyrouz
08-19-2009, 07:47 PM
I like the issue of the nameArray[4], I should have thought about it :D.
Philip you gave me lots of ideas that will make the array go beyond its original length.

(Naughty me) :D

Anyway I forgot to mention that the array in the original post has a syntax error in it:


var Jane=new Array ("Female","Red","Lipstick","28",","45789546");
var Mary=new Array ("Female","Orange","Lipstick","25",","45789547");
var Elle=new Array ("Female","Blue","Lipstick","21",","45789548");


The fourth element of all arrays is an opened double quote without being closed, which will lead to the error of unterminated constant string.

Philip M
08-19-2009, 07:52 PM
Anyway I forgot to mention that the array in the original post has a syntax error in it:


var Jane=new Array ("Female","Red","Lipstick","28",","45789546");
var Mary=new Array ("Female","Orange","Lipstick","25",","45789547");
var Elle=new Array ("Female","Blue","Lipstick","21",","45789548");


The fourth element of all arrays is an opened double quote without being closed, which will lead to the error of unterminated constant string.

This is an example of why it is best to always use double quotes " wherever possible, using escape \" as required if there are quotes within quotes. Otherwise '' (two single quotes) and " (one double quote) are pretty well indistinguishable. For myself I avoid '' (two single quotes) like the plague.

janjan_376
08-19-2009, 08:03 PM
Thank you very much. It worked perfectly. I really appreciated it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum