const _permute = string => { if(string.length === 1) { return [string] } const results = [] for(let s of string){ const arr = string.split('').filter(str =>str !== s) _permute(arr.join('')).forEach(item => { results.push(s + item) }) } return results } console.log(_permute('rap'))