Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. // Find the twos
  2. // Given an integer like this:
  3. // 2468122368128146226989812
  4. // Implement a function called `findTheTwos` that return the total number of number two in the integer
  5. // findTheTwos(246812236812) = 7
  6.  
  7. const num = 246812236812;
  8. const targetNum = 2;
  9.  
  10. function findTheTwos(int, target) {
  11. let totalOcurrences = 0;
  12.  
  13. while (int > 0) {
  14. if (int % 10 === target) {
  15. totalOcurrences += 1;
  16. }
  17.  
  18. int = Math.floor(int / 10);
  19. }
  20.  
  21. return totalOcurrences;
  22. }
  23.  
  24. console.log(findTheTwos(num, targetNum));
  25.  
  26. // Restrictions
  27. // You CAN NOT use strings in any form
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement