homepage Welcome to WebmasterWorld Guest from 54.196.198.213
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

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:4333798
 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:4333802
 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




msg:4333844
 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




msg:4333856
 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




msg:4333862
 9:26 pm on Jul 1, 2011 (gmt 0)

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

case sensitivity: Id rather than ID

TriZzz




msg:4333900
 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




msg:4333919
 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




msg:4333938
 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:4333940
 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




msg:4333963
 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:4333979
 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




msg:4333982
 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




msg:4334709
 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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved