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 6 of 6

Thread: Help With Array

  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help With Array

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

  • #2
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    Try this:
    Code:
    <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>

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    More or less the ckeyrouz's script with a few cosmetic improvements, including avoid document.write().

    Code:
    <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.
    Last edited by Philip M; 08-19-2009 at 06:43 PM.

  • #4
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    I like the issue of the nameArray[4], I should have thought about it .
    Philip you gave me lots of ideas that will make the array go beyond its original length.

    (Naughty me)

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

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

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by ckeyrouz View Post
    Anyway I forgot to mention that the array in the original post has a syntax error in it:

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

  • #6
    New to the CF scene
    Join Date
    Jun 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much. It worked perfectly. I really appreciated it.


  •  

    Posting Permissions

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