Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- firstAndLastKNumbers([2, 7, 8, 9]);
- function firstAndLastKNumbers(arr) {
- let k = arr.shift(); // (3) [7, 8, 9]
- // k = 2 - the removed element from the original array
- let firstLine = arr.slice(0, k) // (2) [7, 8];
- // k shows the count of numbers per line
- let secondLine = arr.slice(arr.length - k, arr.length); // (2) [8, 9]
- // arr.length - k = 3 - 2 = 1 -- the start index of slice function which is 8
- console.log(firstLine.join(' '));
- console.log(secondLine.join(' '));
- }
Advertisement
Add Comment
Please, Sign In to add comment