Hello, how can I check if the variable is 10, 20, 30 and so on. or 100, 200, 300 and so on. Simply put, how to check for the presence of zero and two zeros at the end of a number?

var x; function(){ x++; if (x>9 && /*проверка на ноль в конце*/ || x>99 && /*проверка на два нуля в конце*/) { alert(x); } } 
  • 6
    The remainder of the division by 10 and 100 will definitely save you. - Vladimir Martyanov
  • @ VladimirMartyanov, I could write an answer - Grundy

4 answers 4

It is necessary to check the remainder of the division by the power of tens

 function isDecDivide(ANum) { var res = 10; while (ANum % res == 0) res *= 10; return res / 10; } console.log(isDecDivide(9)); console.log(isDecDivide(90)); console.log(isDecDivide(900)); console.log(isDecDivide(9000)); 

    It is possible through the remainder of the division of %

    Or lead to the string:

      var x = '100010'; x.substr(-2); if (x == '00') { ... } var x = '100010'; x.substr(-1); if (x == '0') { ... } 

      (Bad advice)

      The remainder of the division is too simple and obvious, better use regular season !.

       var x = 1000 var match = x.toString(10).match(/^[1-9]0+$/) if( match !== null ) console.log( x, 'number of zeros', match[0].length - 1 ); 

        Many thanks to everyone, I (oh, fool!) Did not think about the remainder of the division.

         var x = 0; function() { x++; if (x > 0 && x % 10 == 0 && x < 100) { console.log(x); } else if (x % 100 == 0 && x < 1000) { console.log(x); } }