Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-26-2007, 03:31 AM   PM User | #1
1337coder
New Coder

 
Join Date: Sep 2006
Location: Toronto
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
1337coder is an unknown quantity at this point
Addition Operator

In most of my scripts concerning math operators, I use prompts. The bad thing about prompt variables is that, although you can subtract, multiply, and divide them normally, addition treats the variables as strings, and does not add the values. This has proven to be an annoying problem, and although I have found a solution through loops and an if statement, suffice to say it is inconvenient, and does not work with decimals. I noticed that when I declared the variables myself, they are added as numbers not strings, because the variable is a number. Is there a way to make a variable declared through prompt a definite number so addition does not add strings?

PS: By prompt I mean

var a = prompt("Type in a number here", "")
__________________
var currentTime = new Date()
var chance = currentTime.getTime()
var chance2 = chance % 3
alert(chance2)
1337coder is offline   Reply With Quote
Old 08-26-2007, 04:00 AM   PM User | #2
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Code:
var a = Number(prompt("Type in a number here", ""));
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Users who have thanked glenngv for this post:
1337coder (08-26-2007)
Old 08-26-2007, 04:12 AM   PM User | #3
1337coder
New Coder

 
Join Date: Sep 2006
Location: Toronto
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
1337coder is an unknown quantity at this point
Thanks a lot, this saves a lot of time.
__________________
var currentTime = new Date()
var chance = currentTime.getTime()
var chance2 = chance % 3
alert(chance2)
1337coder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:39 AM.


Advertisement
Log in to turn off these ads.