Skip to content

Instantly share code, notes, and snippets.

@anchowake
Last active June 17, 2019 22:15
Show Gist options
  • Select an option

  • Save anchowake/ad7d4fc0c65816545e037672db870b49 to your computer and use it in GitHub Desktop.

Select an option

Save anchowake/ad7d4fc0c65816545e037672db870b49 to your computer and use it in GitHub Desktop.
// Find the twos
// Given an integer like this:
// 2468122368128146226989812
// Implement a function called `findTheTwos` that return the total number of number two in the integer
// findTheTwos(246812236812) = 7
const num = 246812236812;
const targetNum = 2;
function findTheTwos(int, target) {
let totalOcurrences = 0;
while (int > 0) {
if (int % 10 === target) {
totalOcurrences += 1;
}
int = Math.floor(int / 10);
}
return totalOcurrences;
}
console.log(findTheTwos(num, targetNum));
// Restrictions
// You CAN NOT use strings in any form
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment