Q:

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

0

Write a JavaScript program to get removed elements 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 takeWhile = (arr, func) => {
  for (const [i, val] of arr.entries()) if (func(val)) return arr.slice(0, i);
  return arr;
};

console.log(takeWhile([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