Advertisement
Spocoman

08. Special Numbers

Jan 17th, 2022 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function specialNumbers(num) {
  2.     for (let i = 1; i <= num; i++){
  3.         let sum = 0;
  4.         for (let j = 0; j < i.toString().length; j++){
  5.             sum += Number(i.toString()[j]);
  6.         }
  7.         if (sum === 5 || sum === 7 || sum === 11){
  8.             console.log(`${i} -> True`);
  9.         }  else {
  10.             console.log(`${i} -> False`);
  11.         }
  12.     }
  13. }
  14.  
  15. Решение с методи, ламбда и тернарен оператор:
  16.  
  17. function specialNumbers(num) {
  18.     for (let i = 1; i <= num; i++) {
  19.         let sum = i.toString().split('').map(Number).reduce((a, b) => a + b, 0);
  20.         console.log(`${i} -> ${[5, 7, 11].includes(sum) ? 'True' : 'False'}`);
  21.     }
  22. }
  23.  
  24. И тарикатската:)
  25.  
  26. function specialNumbers(num) {
  27.     for (let i = 1; i <= num; i++) {
  28.         console.log(`${i} -> ${[5, 7, 11].includes(i.toString().split('').map(Number).reduce((a, b) => a + b, 0)) ? 'True' : 'False'}`);
  29.     }
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement