Comments

Salah El-Shennawy5 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) };

calculating feed4 months ago

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

Berkshire revolutionary South Carolina5 months ago

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

Human Mouse ivory7 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; };

plum invoice7 months ago

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

Fatih KAYA10 months ago

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

Richard O'Brien10 months ago

This one-liner does the trick

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

Arvind Pala year ago

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

Shekhar Patila year ago

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

Unbranded online Bordersa year ago

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

Unbranded online Bordersa year ago

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

Qamar Khurshida year ago

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

tayebe kavousia 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(); };

`

Mohamed El Ghannay2 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; };

Mohamed El Ghannay2 years ago

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

Mohamed El Ghannay2 years ago

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

Narek Musakhanyan2 years ago

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

tayebe kavousia year ago

this is much better

invoice Avon2 years ago

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

this worked..

Mochammad Andrian Maulana2 years ago

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

Daniel Cortes2 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?

Bacon Chair Palladium2 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]

Bacon Chair Palladium2 years ago

nevermind, I figured it out

benchmark Turkmenistan Granite2 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)

Michael Morgan2 years ago

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

Applications cyan2 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.

DennisLoska2 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 [];
  }, []);
};

Abdulhamid Abdo2 years ago

This is too complicated. I think.