How to check for a decimal point

Discussion in 'JavaScript' started by chricholson, May 13, 2010.

  1. #1
    I am creating a maths game with Javascript by randomly generating two numbers between 1 and 12 and carrying out some mathematics on the two (randomly putting one of: +,-,*,/). Divide proves a problem as obvously two numbers divided can result in awkward answers, recurring 3's for example. Does anyone know how I can check for a decimal point in an answer and if so generate a new equation?

    Many thanks,

    Chris
     
    chricholson, May 13, 2010 IP
  2. unigogo

    unigogo Peon

    Messages:
    286
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    use regular expression, e.g.

    /\./
     
    unigogo, May 21, 2010 IP
  3. wab

    wab Member

    Messages:
    57
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #3
    You can use the % operator.
    if you have 2 values a and b, a%b gives you the remainder, like 4%3 = 1.
    So in your case, that would be something like:
    
    if(a%b==0){
      // do something
    }else{
      // generate new equation
    }
    
    Code (markup):
     
    wab, May 21, 2010 IP