Javascript Number Integer count 2 between two numbers

Introduction

Write a method that counts the number of 2's in all numbers between 0 and n inclusive.

input: 25

output: 9 (2,12,20,21,22,23,24,25)

22 counts for two 2's


var countOfTwos = function(num) {
  if(num < 2) {
    return 0;//  w  w w  .  ja  v a 2s .  c o  m
  }
  var numOfTwos = 0;
  var currentNum;
  for(var i = 2; i <= num; i++) {
    currentNum = i.toString().split('');
    if(currentNum.indexOf('2') !== -1) {
      for(var j = 0; j < currentNum.length; j++) {
        if(currentNum[j] === '2') {
          numOfTwos++;
        }
      }
    }
  }
  return numOfTwos;
}
console.log(countOfTwos(25));



PreviousNext

Related