1

I have problem in if else condition

First,

amt has value of 1000. actualamt has value of 500.

I am receiving the above values from text boxes.

My condition is if actualamt is greater than amt, it will not proceed. But even if the value of actualamt is lower than amt, it didnt accept.

here's my code

if (actualamt > amt) {
  checker = 0;
  $.toast({
    heading: 'Note:',
    text: "You have exceeded amsount for settle. Please check the amount.",
    icon: 'info',
    loader: false,
    stack: false,
    position: 'top-center',
    bgColor: '#FFA500',
    textColor: 'white',
    allowToastClose: false,
    hideAfter: 4000
  });
}

Can you help me with this. Thanks

Harshith Rai
  • 3,018
  • 7
  • 22
  • 35
Yettt
  • 21
  • 1
  • 10
  • 4
    Are those variables actually integer values or are they strings? Moreover, your if condition does exactly the opposite of what you are saying--> It will proceed if actualamt is greater than amt – Krishna Prashatt Mar 07 '19 at 09:42
  • 4
    do you have strings as value? – Nina Scholz Mar 07 '19 at 09:42
  • I'm getting the value from a textbox – Yettt Mar 07 '19 at 09:42
  • Five hundred is **LESS THAN** one thousand! – Quentin Mar 07 '19 at 09:43
  • 2
    `if (parseInt(actualamt) > parseInt(amt) ){` Change your condition by this, – HarisH Sharma Mar 07 '19 at 09:43
  • change the text box from `input type = text` to `input type = number` – Harshith Rai Mar 07 '19 at 09:43
  • 1
    Possible duplicate of [Javascript string/integer comparisons](https://stackoverflow.com/questions/5630123/javascript-string-integer-comparisons) and [issue with comparing two numbers in javascript](https://stackoverflow.com/questions/9094299) and [javascript if number greater than number](https://stackoverflow.com/questions/13079626) and [IF Greater not working well in js](https://stackoverflow.com/questions/36967730) – adiga Mar 07 '19 at 09:45
  • 1
    Using parseInt works! Thanks guys for helping me, especially @harishsharma. – Yettt Mar 07 '19 at 09:47
  • @harishsharma — Never use parseInt without a radix argument. – Quentin Mar 07 '19 at 09:48
  • This post is leading towards parseInt vs unary plus argument. Check [this](https://stackoverflow.com/a/17106702/1955268) for more details on it. – prinkpan Mar 07 '19 at 09:54

1 Answers1

-1

For comparing numerical values, you could convert the strings to a number with an unary plus +. This does not hurt, if you have already numbers.

if (+actualamt > +amt) {
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 2
    @YosvelQuintero, it looks like some people follows a herd behaviour and sometimes need a more complicated result as js offers, like `+` vs `parseFloat`/`parseInt`. as long as this language offers this operands with the implicit type conversion, i take this. – Nina Scholz Mar 07 '19 at 10:04
  • Thank you very much for your prompt response.. Learning with this QA [parseInt vs unary plus - when to use which](https://stackoverflow.com/questions/17106681/parseint-vs-unary-plus-when-to-use-which/17106702#17106702) ;) – Yosvel Quintero Mar 07 '19 at 10:10