Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function numbersDivisibleOf9(input) {
- let startNum = Number(input[0]);
- let finalNum = Number(input[1]);
- let numbers = '';
- let sum = 0;
- for (let i = startNum; i <= finalNum; i++) {
- if (i % 9 === 0) {
- sum += i;
- numbers += i + '\n';
- i += 8;
- }
- }
- console.log(`The sum: ${sum}\n${numbers}`);
- }
- РЕШЕНИЕ С МАСИВ И ЛАМБДА:
- function numbersDivisibleOf9(input) {
- let startNum = Number(input[0]);
- let finalNum = Number(input[1]);
- let numbers = [];
- for (let i = startNum; i <= finalNum; i++) {
- if (i % 9 === 0) {
- numbers.push(i);
- i += 8;
- }
- }
- console.log(`The sum: ${numbers.reduce((a, b) => a + b, 0)}\n${numbers.join('\n')}`);
- }
- ИЛИ:
- function numbersDivisibleOf9(input) {
- let startNum = Number(input[0]);
- let finalNum = Number(input[1]);
- let numbers = Array(finalNum - startNum + 1).fill().map((_, i) => startNum + i).filter(n => n % 9 === 0);
- console.log(`The sum: ${numbers.reduce((a, b) => a + b, 0)}\n${numbers.join('\n')}`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement