Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement