From 648ec5645994a46bb6f22aa962671ce4a87c0a2b Mon Sep 17 00:00:00 2001 From: King Date: Mon, 12 Feb 2018 19:22:11 -0500 Subject: [PATCH] ran npm run tester generate js test files and refactored permuteAll --- snippets/permuteAll.md | 20 +++++++++----------- test/permuteAll/permuteAll.js | 16 +++++++--------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/snippets/permuteAll.md b/snippets/permuteAll.md index 63cde20da..746b964e9 100644 --- a/snippets/permuteAll.md +++ b/snippets/permuteAll.md @@ -7,20 +7,18 @@ const permuteAll = (input) => { const result = []; let inputState = input; - if (typeof input === 'string') inputState = input.split('') + if (typeof input === 'string') inputState = input.split(''); if (typeof input === 'number') inputState = (input).toString().split(''); const permute = (arr, m = []) => { - if (arr.length === 0) { - result.push(m) - } else { - for (let i = 0; i < arr.length; i++) { - let curr = arr.slice(); - let next = curr.splice(i, 1); - permute(curr.slice(), m.concat(next)) - } - } - } + (arr.length === 0) + ? result.push(m) + : arr.forEach((_, i) => { + let curr = arr.slice(); + let next = curr.splice(i, 1); + permute(curr.slice(), m.concat(next)); + }); + }; permute(inputState); diff --git a/test/permuteAll/permuteAll.js b/test/permuteAll/permuteAll.js index 672c7a094..facdd309b 100644 --- a/test/permuteAll/permuteAll.js +++ b/test/permuteAll/permuteAll.js @@ -2,20 +2,18 @@ const permuteAll = (input) => { const result = []; let inputState = input; -if (typeof input === 'string') inputState = input.split('') +if (typeof input === 'string') inputState = input.split(''); if (typeof input === 'number') inputState = (input).toString().split(''); const permute = (arr, m = []) => { -if (arr.length === 0) { -result.push(m) -} else { -for (let i = 0; i < arr.length; i++) { +(arr.length === 0) +? result.push(m) +: arr.forEach((_, i) => { let curr = arr.slice(); let next = curr.splice(i, 1); -permute(curr.slice(), m.concat(next)) -} -} -} +permute(curr.slice(), m.concat(next)); +}); +}; permute(inputState);