www.webdeveloper.com
Results 1 to 4 of 4

Thread: NEW to JavaScript and I Need help with a simple code!

  1. #1
    Join Date
    Oct 2010
    Posts
    17

    Question NEW to JavaScript and I Need help with a simple code!

    This is my code below. I am trying to match the number var with the name var that is prompted in the browser... for example, if i type in Fran, i want to get Fran's number, but no matter who's name i put in i always get bob's number which is the first number...what am i doing wrong??? It is a simple code, but i dont know what im doing... Thanks




    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    </head>

    <body>

    <script type="text/javascript">
    // Header
    document.write("<h1>Directory Lookup</h1>");
    // Variables and Arrays for names and numbers
    var name = new Array();
    name[0] = "Bob";
    name[1] = "Jane";
    name[2] = "Billy";
    name[3] = "Fran";
    name[4] = "Rex";
    var number = new Array();
    number[0] = "555-308-1232";
    number[1] = "453-346-3299";
    number[2] = "219-233-9832";
    number[3] = "453-282-7628";
    number[4] = "324-339-1193";

    // Variable for name propmt
    var nameS = prompt("Enter the name you are looking for!","Enter Here");

    var count = 0;
    for (count in name)
    {
    if (name[count] == nameS);
    }

    // Alert pop up with name and number
    alert(nameS + number[count]);
    // What appears in HTML browser
    document.write("<p>" + nameS + number[count] + "</p>");

    </script>

    </body>
    </html>

  2. #2
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    374
    Code:
    var count=0
    while ((count < name.length)  && (nameS != name[count]))
      count++;
    if (count == name.length)
      alert("No entry for ",nameS);
    else
      document.write("<p>" + nameS + number[count] + "</p>");
    should do it.

  3. #3
    Join Date
    Oct 2010
    Posts
    17
    thanks, but it isn't working for me for some reason...

  4. #4
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    374
    Code:
    // Variable for name propmt
    var nameS = prompt("Enter the name you are looking for!","Enter Here");
    var count=0
    while ((count < name.length)  && (nameS != name[count]))
      count++;
    if (count == name.length)
      alert("No entry for " + nameS);
    else
      document.write("<p>" + nameS + " " + number[count] + "</p>");

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles