Comparison Operators

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smartic
    New Member
    • May 2007
    • 150

    Comparison Operators

    I'm having problem with this code:

    Code:
    function check(){
    	element1 = 2;
    	element2= 110;
    	if(element1 > element2){
    		alert('Sorry, element1 should be less than element2');
    	}
    }
    what is the problem in this code ???
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    ?? where is the problem ?? ... 2 isn't greater then 110 so you don't get the alert ...

    Comment

    • smartic
      New Member
      • May 2007
      • 150

      #3
      i know that 2 isn't greater than 110 but i get the alert message how can i solve this problem I'm getting mad that is my real code:
      Code:
      function is_numeric(mixed_var){
          return !isNaN(mixed_var);
      }
      if(is_numeric(value) && is_numeric(compv)){
      	if(condi == '<' && (compv < value)){
      		forError(this.elemID,'Sorry, '+elemID+' should be less than '+compn);
      	}else if(condi == '>' && (compv > value)){
      		forError(this.elemID,'Sorry, '+elemID+' should be greater than '+compn);
      	}else if(condi == '>=' && (compv >= value)){
      		forError(this.elemID,'Sorry, '+elemID+' should be greater than or equal '+compn);
      	}else if(condi == '<=' && (compv <= value)){
      		forError(this.elemID,'Sorry, '+elemID+' should be less than or equal '+compn);
      	}
      }else{
      	forError(this.elemID,'Sorry, numeric letters only');
      }

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        so you just posted the wrong code ... but i think your problem is that you in fact compare strings like that:

        Code:
        element1 = '2';
        element2 = '110';
        
        if (element1 > element2) {
            alert('Sorry, element1 should be less than element2');
        }
        so you just need to parseInt() or parseFloat() the values.

        kind regards

        Comment

        • smartic
          New Member
          • May 2007
          • 150

          #5
          thank you for your replay gits that was my problem.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            no problem ... you're welcome ...

            kind regards

            Comment

            Working...