Welcome to WebmasterWorld Guest from 54.146.201.80

Forum Moderators: open

Message Too Old, No Replies

New to JavaScript and need help

This is picking random numbers from an array.

     
7:14 pm on Jul 1, 2011 (gmt 0)

New User

joined:July 1, 2011
posts: 5
votes: 0


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>Test</title>
</head>
<body>
<script type="text/javascript">

names = ["Evon","Justin","Allison","Layla","Baxter","Gizmo","Skyhler","Bentley","Gadget"]
var ranNum = names[Math.floor(Math.random() * names.lengh)]

for(var i=0; i<5; i++){

document.write(names[ranNum] + "<br />");
}


</script>
</body>
</html>


I just keep getting 5 lines of "undefined" when I run it, but I can't figure out why "ranNum" keeps not being defined. Any help would be appreciated...I'm VERY new to JavaScript.
7:23 pm on July 1, 2011 (gmt 0)

New User

joined:July 1, 2011
posts: 5
votes: 0


I'd prefer if the help came in pointing me to the solution, rather than giving it to me. I'd like to figure out what I'm doing wrong...since I'm trying to learn.

Thanks for any help that may come.
8:47 pm on July 1, 2011 (gmt 0)

Preferred Member

5+ Year Member

joined:Aug 18, 2008
posts:408
votes: 0


names.lengh

you are missing a "t" there, and also need to remove one of the names[] as you are looking for names[names[Gizmo]] (or whichever of the names is pulled randomly) essentially and does not exist.
9:13 pm on July 1, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12716
votes: 244


names[ranNum]
unpacks to
names[names[Math.floor(Math.random() * names.lengh)]]

I'm atrocious at js myself* but I see two issues there.


* "Why doesn't this ### 'getElementByID' work?"
9:26 pm on July 1, 2011 (gmt 0)

Preferred Member

5+ Year Member

joined:Aug 18, 2008
posts:408
votes: 0


* "Why doesn't this ### 'getElementByID' work?"

case sensitivity: Id rather than ID
10:27 pm on July 1, 2011 (gmt 0)

New User

joined:July 1, 2011
posts: 5
votes: 0


I figured it out! It was the missing "t" in length and moving the var ranNum inside of the loop.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>Assignment 3b</title>
</head>
<body>
<script type="text/javascript">

names = ["Evon","Justin","Allison","Layla","Baxter","Gizmo","Skyhler","Bentley","Gadget"];


for(var i=0; i<5; i++){
var ranNum = names[Math.floor(Math.random() * names.length)];
document.write([ranNum] + "<br />");
}

</script>
</body>
</html>
11:01 pm on July 1, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


Not sure what browser(s) you are using, but Firefox is generally a lot more helpful than say IE for debugging JavaScript. More meaningful error messages, etc.
11:51 pm on July 1, 2011 (gmt 0)

Preferred Member

5+ Year Member

joined:Aug 18, 2008
posts:408
votes: 0


note that you don't need the brackets [] around ranNum, in so doing you are actually creating an array with ranNum as the only item in the array.
12:06 am on July 2, 2011 (gmt 0)

New User

joined:July 1, 2011
posts: 5
votes: 0


@penders: I'm on a Mac, so I'm using Chrome, Safari and Firefox.

@astupidname: in the document.write or around the math.floor?
1:05 am on July 2, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:July 3, 2006
posts: 3123
votes: 0


@astupidname: in the document.write or around the math.floor?


in the document.write
1:42 am on July 2, 2011 (gmt 0)

New User

joined:July 1, 2011
posts: 5
votes: 0


Thanks for the help! I'm excited to learn more. I appreciate all of the input! Hopefully I'll be back in here to share my experience with others.
1:50 am on July 2, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12716
votes: 244


* "Why doesn't this ### 'getElementByID' work?"

case sensitivity: Id rather than ID

Er, yes, that was my point :P
8:49 am on July 4, 2011 (gmt 0)

Full Member

5+ Year Member

joined:Jan 4, 2007
posts:221
votes: 0


I'm only just getting into JS myself, but if I was taking over that code, from the way the variable is named I'd expect ranNum to contain a number (e.g. the result of "Math.floor(Math.random() * names.length)") to be used to retrieve the appropriate name from the array at a later point ("names[ranNum]").

Okay, so it's trivial to work out that that isn't the case at the moment. But what if the code grows?
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members