/    Sign up×
Community /Pin to ProfileBookmark

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>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@wbportOct 25.2010 — [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>");[/CODE]
should do it.
Copy linkTweet thisAlerts:
@unstable_nesauthorOct 25.2010 — thanks, but it isn't working for me for some reason...
Copy linkTweet thisAlerts:
@wbportOct 25.2010 — [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>");[/CODE]
×

Success!

Help @unstable_nes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...