This solution is generated by ChatGPT, correctness is not guaranteed. You might also be able to find a solution fromcommunity posts.

AI solution for JavaScript Coding Question on
122. implement memoizeOne()

/** * @param {function} fn * @param {function} [isEqual] * @return {function} */function memoizeOne(fn, isEqual = shallowArrayEquality) {  let lastArgs = null;  let lastResult = null;    return function(...args) {    if (!lastArgs || !isEqual(args, lastArgs)) {      // calculate and cache new result      lastResult = fn(...args);      lastArgs = args;    }    // return cached result    return lastResult;  }}function shallowArrayEquality(arr1, arr2) {  if (arr1.length !== arr2.length) return false;  for (let i = 0; i < arr1.length; i++) {    if (arr1[i] !== arr2[i]) return false;  }  return true;}

The memoizeOne() function accepts a function and an optional isEqual function, and it returns a new function that caches the result for the latest arguments only.

The implementation uses two variables, lastArgs and lastResult, to keep track of the latest arguments and result. When the new arguments are received, it checks if they are the same as the last arguments using the isEqual() function. If they are different, the target function is called with the new arguments and the result is cached along with the arguments. If they are the same, the cached result is returned directly.

By default, the isEqual() function does a shallow comparison on the array items using strict equality. The user can provide their own isEqual() function for more specific comparison criteria.