const generator = function * (arr, k) { while (k--) { const toSwitchIndex = Math.floor(Math.random() * arr.length) const temp = arr[toSwitchIndex] arr[toSwitchIndex] = arr[arr.length - 1] arr[arr.length - 1] = temp yield arr.pop() } } const gen = generator(['a', 'b', 'c'], 3) console.log(...gen)