JSFiddle - React, Tailwind, and code Playground

by gianlucaguarini

JavaScript

const list = [1, 2, 3, 4, 5];
const res = [3, 2, 6];

function compare(l1, l2) {
	l1.forEach((el, i) => {
  	if (l2[i] !== el) throw 'the two list are not the same'
  })
}

function updateList(source, result) {
	result.forEach((item, i) => {
    if (item !== source[i]) {
    	source.splice(i, 0, item)
    }
  })
  
  return source.slice(0, result.length)
}

console.log(updateList(list, res))

compare(res, updateList(list, res))