Jump to content

Variable not counted as a number


Sansespere

Recommended Posts

Hi, in my code (which is almost finished) I have a small problem. I am making a variable d1 equal to the sum of variables c1 and b1, however the variable b1 is not being defined as numeric variable and thus when I ask for the sum it is taking the + and instead placing the two numbers side by side as if they were strings. My code looks like this:

function rolladxb1 () { var a1 = document.getElementById("a1").value; var x1 = document.getElementById("x1").value; var b1 = document.getElementById("b1").value; var c1 = 0; for (var i = 0; i <= (a1 - 1); i++) {  c1 = (Math.floor(Math.random()*x1) + 1) + c1; } var d1 = c1 + b1; alert(d1 +"="+ c1 +"+"+ b1)}

I suppose that by defining the variable b1 as the value of an input field, the user can make it anything she desires, thus by default the script is determining that b1 is instead a string. I plan to limit the input field through use of alerts to just numeric values, however I doubt the script would recognize such limits and would still see b1 as a string. How can I make b1 be seen as a numeric value and be able to be added?

Link to comment
Share on other sites

I have a finished product that functions as I want it to. The styles are off in IE, but it works fine in FF. I am going to take it over to CSS forums if I can't figure it out. I would just like to say Thanks a bunch guys! This was my first project using html, css, and js and I am really excited. If you would like a copy of the code so you can see it you can PM me, because I currently can't host it to the web.Thanks again,

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...