Computing.Net > Forums > Web Development > Javascript: Math.random prob.

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Javascript: Math.random prob.

Reply to Message Icon

Name: mie2com
Date: August 14, 2007 at 12:33:10 Pacific
OS: XP
CPU/Ram: Pentium 3
Product: Dell
Comment:

i'm trying to make this script alert random text based on random numbers. however, it keeps alert the same word. what is the prob. here's the script.

function test() {
x = Math.floor(Math.random()*11);
if (x=0) {
alert("Below")
} else if (x<5) {
alert("Middle")
} else {
alert("Above")
}
}
</script>
[a href="javascript:void(0)" onclick="test()"]Click Me[/a]



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: August 14, 2007 at 14:02:54 Pacific
Reply:

The problem is the first condition statement:

if (x=0) {

It is testing to see if it can set x to the value of 0, not whether x equals 5. To test for equality you need to use double equal signs:

if (x==0) {


Michael J


0

Response Number 2
Name: mie2com
Date: August 14, 2007 at 14:35:47 Pacific
Reply:

cool. thanks again MichaelJ. i was on rushing. many thanks. :D


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Javascript: Math.random prob.

javascript background random image www.computing.net/answers/webdevel/javascript-background-random-image-/3613.html

Confict using two JavaScript tools www.computing.net/answers/webdevel/confict-using-two-javascript-tools/2036.html

javascript security www.computing.net/answers/webdevel/javascript-security/3561.html