Skip to content

Instantly share code, notes, and snippets.

@JimRottinger
Last active May 7, 2019 01:11
Show Gist options
  • Save JimRottinger/77a7a929c9db23525a1a9c2b1b28815e to your computer and use it in GitHub Desktop.
Save JimRottinger/77a7a929c9db23525a1a9c2b1b28815e to your computer and use it in GitHub Desktop.
const checkLoopInvariant = (newArr, originalArr, index) => {
//need to slice at least 1 element out
if (index < 1) index = 1
newArr = newArr.slice(0,index)
originalArr = originalArr.slice(0, index)
for (let i=0; i < newArr.length; i++) {
//check that the original array contains the value
if (!originalArr.includes(newArr[i])) {
console.error(`Failed! Original array does not include ${newArr[i]}`)
}
//check that the new array is in sorted order
if (i < newArr.length - 1 && newArr[i] > newArr[i+1]) {
console.error(`Failed! ${newArr[i]} is not less than ${newArr[i+1]}`)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment