Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function specialNumbers(num) {
- for (let i = 1; i <= num; i++){
- let sum = 0;
- for (let j = 0; j < i.toString().length; j++){
- sum += Number(i.toString()[j]);
- }
- if (sum === 5 || sum === 7 || sum === 11){
- console.log(`${i} -> True`);
- } else {
- console.log(`${i} -> False`);
- }
- }
- }
- Решение с методи, ламбда и тернарен оператор:
- function specialNumbers(num) {
- for (let i = 1; i <= num; i++) {
- let sum = i.toString().split('').map(Number).reduce((a, b) => a + b, 0);
- console.log(`${i} -> ${[5, 7, 11].includes(sum) ? 'True' : 'False'}`);
- }
- }
- И тарикатската:)
- function specialNumbers(num) {
- for (let i = 1; i <= num; i++) {
- console.log(`${i} -> ${[5, 7, 11].includes(i.toString().split('').map(Number).reduce((a, b) => a + b, 0)) ? 'True' : 'False'}`);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement