Compare two number using JavaScipt (Amateur question)

Discussion in 'JavaScript' started by cre8ive, Feb 13, 2008.

  1. #1
    I want to compare two number of using JavaScipt and find a bigger number. Specifically, I have the following two numbers:

    adjustY-100
    40

    I want to compare these two and find a bigger number. Is there a function to do this ...? Or I need to use an operator to find the bigger number?
     
    cre8ive, Feb 13, 2008 IP
  2. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    :confused:
     
    MMJ, Feb 13, 2008 IP
  3. Simon.Rain

    Simon.Rain Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You would need to actually have 2 input field and and a button to compare
    and then replace the values with their element names
    ex.
    x = document.form1.name1.value
    y = document.form1.name2.value

    and the button would be there to activate the function

    function compare() {

    var x=100
    var y=40

    if ( x > y ){
    document.write("x is the bigger number");
    }
    else{
    document.write("y is the bigger number");
    }
    }
     
    Simon.Rain, Feb 13, 2008 IP
  4. locdev

    locdev Active Member

    Messages:
    171
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Function should be:
    function compare() {

    var x=100;
    var y=40;

    if ( x > y ){
    document.write("x is the bigger number");
    }
    else{
    if(x==y){
    document.write("y is equal to x");
    }else{
    document.write("y is the bigger number");
    }

    }
    }
     
    locdev, Feb 13, 2008 IP
  5. vpguy

    vpguy Guest

    Messages:
    275
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you only need the larger of the two values you can do it like this:
    biggest = Math.max(AdjustY - 100, 40);
    Code (markup):
     
    vpguy, Feb 15, 2008 IP