Advertisement
vladovip

JS - Special numbers

Dec 26th, 2021
1,085
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(n) {
  2.  
  3.     let i = 1;
  4.  
  5.     while (i <= n) {
  6.  
  7.         let sumOfDigids = 0;
  8.         let currentStrNumber = i.toString();
  9.  
  10.         for (let j = 0; j < currentStrNumber.length; j++) {
  11.             sumOfDigids += Number(currentStrNumber[j]);
  12.         }
  13.  
  14.         if (sumOfDigids === 5 || sumOfDigids === 7 || sumOfDigids === 11) {
  15.             console.log(`${currentStrNumber} -> True`);
  16.         } else {
  17.             console.log(`${currentStrNumber} -> False`);
  18.         }
  19.  
  20.         i++;
  21.     }
  22.  
  23.  
  24. }
  25.  
  26. solve(15);
  27.  
  28.  
  29. /* Write a program to receive a number n and for all numbers in the range 1…n print the number
  30.  and if it is special or not (True / False).
  31. A number is special when its sum of digits is 5, 7 or 11.
  32. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement