5 days ago

first solution => const solve = (intArray, k) => { return intArray.reverse().splice(0, k).reverse() };

second solution => const solve = (intArray, k) => { return intArray.splice(intArray.length - k, k) };

4 months ago

const solve = (intArray, k) => { return intArray.splice(intArray.length-k,k); };

5 months ago

const solve = (intArray, k) => { let newArray = []; for (i = intArray.length - k; i <= intArray.length - 1; i++) { newArray.push(intArray[i]); } return newArray; };

7 months ago

const solve = (intArray, k) => { let r = intArray.length-k; let res; for(let i=r;i<=intArray.length-1;i++){ res = console.log(intArray[i]); } return res; };

7 months ago

return intArray.slice(k - intArray.length);

10 months ago

const solve = (intArray, k) => { let newArr = intArray.reverse().slice(0,k).reverse(); return newArr; };

10 months ago

This one-liner does the trick

const solve = (intArray, k) => intArray.slice(intArray.length - k);

a year ago

const solve = (intArray, k) => {
let arrLength = intArray.length;
return intArray.filter((e,index)=>index>=arrLength-k);
};

a year ago

const solve = (intArray, k) => intArray.slice(-k);

a year ago

const solve = (intArray, k) => intArray.slice(k * -1);

a year ago

const solve = (intArray, k) => { const arrLen = intArray.length; for(let i = 0; i < arrLen - k; i++) { intArray.shift(); } return intArray; };

a year ago

const solve = (intArray, k) => {
return intArray.reverse().slice(0,k).reverse();
};

a year ago

this is my code with for loop `const solve = (intArray, k) => { let result = []; for (let i = 0 ; i < k;i++){ let n = intArray.length -1; result.push(intArray[n-i]); } return result.reverse(); };

`

2 years ago

my solution using unshift Array method: ``const solve = (intArray, k) => { let result = []; let index = intArray.length - 1; while (k > 0) { result.unshift(intArray[index]); k--; index--; } return result; };

2 years ago

``const solve = (intArray, k) => { let result = []; let index = intArray.length - 1; while (k > 0) { result.unshift(intArray[index]); k--; index--; } return result; };

2 years ago

const solve = (intArray, k) => { let result = []; let index = intArray.length - 1; while (k > 0) { result.unshift(intArray[index]); k--; index--; } return result; };

2 years ago

const solve = (intArray, k) => {
return intArray.slice(-k);
};

a year ago

this is much better

2 years ago

const solve = (intArray, k) => {
return intArray.slice(-k);
};

this worked..

2 years ago

const solve = (intArray, k) => intArray.splice(intArray.length-k,k);

2 years ago

Here is my solution:

const solve = (intArray, k) => {
let index = 2;
while (index < intArray.length) {
return results = intArray.slice(index, index + k);
index += k;
}
};

I am being told its not quite right because its returning [3,4] instead of [1,2,3,4]. Now I am confused, is that not what Example # 1 asks us to return?

2 years ago

I'm not sure why slice(k) does not work because if I try it on console [1,2,3,4].slice(2), it returns [3,4]

2 years ago

nevermind, I figured it out

2 years ago

my solution with [array.prototype.slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) :

const solve = (intArray, k) => intArray.slice(-k)

2 years ago

Here is my simple solution: const solve = (intArray, k) => { return intArray.splice(intArray.length - k); };

2 years ago

Interesting I used slice and they both work the same. The only difference appears to be that slice creates an new array and splice mutates the original array.

2 years ago

Here's my solution. The tricky part was to not forget, that you ALWAYS need to return something in the callback of your reduce() function, otherwise you will wonder like me, why your accumulator stays undefined.

const solve = (intArray, k) => {
return intArray.reduce((numbers, current, i) => {
if (i >= intArray.length - k) {
numbers.push(current);
return numbers;
} else return [];
}, []);
};

2 years ago

This is too complicated. I think.