Advertisement
PowerCell46

First and last K numbers JS

Nov 28th, 2022
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function firstAndLastKnumbers(array) {
  2.   let numberK = array[0];
  3.   let arrayFirst = [];
  4.   let arraySecond = [];
  5.   let index2 = Number(array.length);
  6.   for (let index = 1; index <= numberK; index++) {
  7.     let currentNum = array[index];
  8.     arrayFirst.push(currentNum);
  9.   }
  10.  
  11.   for (let index1 = 1; index1 <= numberK; index1++) {
  12.     index2--;
  13.     let currentNum1 = array[index2];
  14.     arraySecond.unshift(currentNum1); // the next number will push the one in front and it'll become first in the array
  15.   }
  16.  
  17.   console.log(arrayFirst.join(" "));
  18.   console.log(arraySecond.join(" "));
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement