Advertisement
Spocoman

06. Print And Sum

Jan 13th, 2022
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function printAndSum(startNum, endNum) {
  2.     let output = '';
  3.     let sum = 0;
  4.     for (let i = startNum; i <= endNum; i++) {
  5.         output += i + ' ';
  6.         sum += i;
  7.     }
  8.  
  9.     console.log(output);
  10.     console.log(`Sum: ${sum}`);
  11.  
  12. }
  13.  
  14.  
  15. Адванс решение:
  16.  
  17. function printAndSum(startNum, endNum) {
  18.  
  19.     let array = Array(endNum - startNum + 1).fill().map((_, i) => startNum + i);
  20.     console.log(`${array.join(' ')}\nSum: ${array.reduce((a,b) => a+b)}`);
  21.  
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement