Welcome to WebmasterWorld Guest from 50.19.156.133

Forum Moderators: open

Message Too Old, No Replies

New to JavaScript and need help

This is picking random numbers from an array.

     

TriZzz

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

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

8:47 pm on Jul 1, 2011 (gmt 0)

5+ Year Member



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

9:13 pm on Jul 1, 2011 (gmt 0)

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



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

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

5+ Year Member



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

case sensitivity: Id rather than ID

TriZzz

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

11:01 pm on Jul 1, 2011 (gmt 0)

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



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

11:51 pm on Jul 1, 2011 (gmt 0)

5+ Year Member



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

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

1:05 am on Jul 2, 2011 (gmt 0)

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



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


in the document.write

TriZzz

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

1:50 am on Jul 2, 2011 (gmt 0)

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



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

case sensitivity: Id rather than ID

Er, yes, that was my point :P

Status_203

8:49 am on Jul 4, 2011 (gmt 0)

5+ Year Member



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?
 

Featured Threads

Hot Threads This Week

Hot Threads This Month