Topic Description

Create a difference function that takes two arrays as arguments and returns an array containing the elements that are present in the first array but not in the second array. The function should avoid redundant operations on duplicate values.

Create a dropWhile function that takes two parameters:

The dropWhile function should return a new array without modifying the original array. The returned array should start from the first element in the original array that does not satisfy the predicate condition and include all subsequent elements until the end of the array. If every element satisfies the predicate function, an empty array should be returned.

console.log(dropWhile([1, 2, 3, 4, 5, 6], (value) => value < 4)); // => [4, 5, 6]

console.log(dropWhile([0, 1, 2], (value) => value < 5)); // => []

console.log(dropWhile([0, 6, 1, 2], (value) => value < 5)); // => [6, 1, 2]

Answer


function dropWhile(arr,predicate){
	let index = 0
	for(item of arr){
		if(!predicate(item)){
			break
		}else{
			index++
		}
	}
	return arr.slice(index)
}
function dropWhile(array, predicate) {
  let index = 0 

  while (index < array.length && predicate(array[index])) {
    index++ 
  }

  return array.slice(index)
}