Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please modify the following program in JAVASCRIPT, so that instead of printing t

ID: 3744936 • Letter: P

Question

Please modify the following program in JAVASCRIPT, so that instead of printing the permutations of the letters in an input word, it prints the permutations of the words in an input phrase.

function generatePermutations(a, n) { if (n === 0) {
console.log(a.join('')); } else {
for (let i = 0; i < n; i++)
{
generatePermutations(a, n - 1);
const j = n % 2 === 0 ? 0 : i;
[a[j], a[n]] = [a[n], a[j]];
}
generatePermutations(a, n - 1);
}
}
if (process.argv.length !== 3) {
console.error('Rat' 'Cat''Mat');
process.exit(1);
}
const word = process.argv[2];
generatePermutations(word.split(''), word.length - 1);

Explanation / Answer

Please find the code below


var word=[]; // changed constant word to word array to recevice arguments in it from 2 index to last index

if (process.argv.length < 3) {
console.error('Enter command line argument'); // checked wether argument size is greater than < 3
process.exit(1);
}
for (let j = 2; j < process.argv.length; j++) {  
word.push(process.argv[j]); // fill the word aaray with argument one by one
}
generatePermutations(word, word.length - 1); // send word array insted of character array and length of word array

function generatePermutations(a, n) { if (n === 0) {
console.log(a.join(' ')); // placing place between two words
} else {
for (let i = 0; i < n; i++)
{
generatePermutations(a, n - 1);
const j = n % 2 === 0 ? 0 : i;
[a[j], a[n]] = [a[n], a[j]];
}
generatePermutations(a, n - 1);
}
}

Please like my answer if its correct.

Thank you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote