1
var pancakeSort = function (arr) {4
for (let i = arr.length; i > 0; i--) {5
//search the array for all values from 1 to n6
let idx = arr.indexOf(i);8
// if value is not present at its desired index9
let pancake = arr.slice(0, idx + 1).reverse(); //flip the array with k=index of value i to put it in front of the array11
arr = arr.slice(idx + 1);12
arr = pancake.concat(arr); //value i is now at index 013
pancake = arr.slice(0, i).reverse(); //flip the array with k = i-1 to put the value at its place16
arr = pancake.concat(arr); //now the array is sorted from i to n