homepage Welcome to WebmasterWorld Guest from 54.227.215.139
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
New to JavaScript and need help
This is picking random numbers from an array.
TriZzz



 
Msg#: 4333796 posted 7:14 pm on Jul 1, 2011 (gmt 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.

 

TriZzz



 
Msg#: 4333796 posted 7:23 pm on Jul 1, 2011 (gmt 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.

astupidname

5+ Year Member



 
Msg#: 4333796 posted 8:47 pm on Jul 1, 2011 (gmt 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.

lucy24

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



 
Msg#: 4333796 posted 9:13 pm on Jul 1, 2011 (gmt 0)

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?"

astupidname

5+ Year Member



 
Msg#: 4333796 posted 9:26 pm on Jul 1, 2011 (gmt 0)

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

case sensitivity: Id rather than ID

TriZzz



 
Msg#: 4333796 posted 10:27 pm on Jul 1, 2011 (gmt 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>

penders

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



 
Msg#: 4333796 posted 11:01 pm on Jul 1, 2011 (gmt 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.

astupidname

5+ Year Member



 
Msg#: 4333796 posted 11:51 pm on Jul 1, 2011 (gmt 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.

TriZzz



 
Msg#: 4333796 posted 12:06 am on Jul 2, 2011 (gmt 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?

penders

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



 
Msg#: 4333796 posted 1:05 am on Jul 2, 2011 (gmt 0)

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


in the document.write

TriZzz



 
Msg#: 4333796 posted 1:42 am on Jul 2, 2011 (gmt 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.

lucy24

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



 
Msg#: 4333796 posted 1:50 am on Jul 2, 2011 (gmt 0)

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

case sensitivity: Id rather than ID

Er, yes, that was my point :P

Status_203

5+ Year Member



 
Msg#: 4333796 posted 8:49 am on Jul 4, 2011 (gmt 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?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved