Q:

Write a JavaScript program to get removed elements from the end of a given array until the passed function returns true

0

Write a JavaScript program to get removed elements from the end of a given array until the passed function returns true.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

const takeRightWhile = (arr, func) => {
  for (let i of arr.reverse().keys())
    if (func(arr[i])) return arr.reverse().slice(arr.length - i, arr.length);
  return arr;
};

console.log(takeRightWhile([1, 2, 3, 4], n => n < 3));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now