georgiev955

task 2

Sep 15th, 2023
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.36 KB | None | 0 0
  1. function lastKNum(n, k) {
  2. let result = [1];
  3. while (result.length < n) {
  4. let nextNum = 0;
  5. if (result.length < k) {
  6. nextNum = result.reduce((a, b) => a + b);
  7. } else {
  8. nextNum = result.slice(result.length - k).reduce((a, b) => a + b);
  9. }
  10. result.push(nextNum);
  11. }
  12. return result;
  13. }
Advertisement
Add Comment
Please, Sign In to add comment