185k views
4 votes
1. Create a pseudocode program that asks students to enter a word. Call a function to compute the different ways in which the letters that make up the word can be arranged.

2. Convert the pseudocode in question one to JavaScript and test your program.

User Sudheeshix
by
5.1k points

1 Answer

2 votes

Answer:

1)Pseudocode:-

Prompt user to enter string

Pass it to combination function which can calculate the entire combination and can return it in an array

In combination method -

Check if the string is length 2 size ie of 0 or 1 length then return string itself

Else iterate through string user entered

Get the present character within the variable and check if the variable is already used then skip

Create a string from 0 to i the index + (concatenate) i+1 to length and recursive call combination function and pass this string and store end in subpermutation array

For each recursive call push character + sub permutation in total arrangement array.

2)Code -

function combinations(userStr) {

/* when string length is less than 2 then simply return string */

if (userStr.length < 2) return userStr;

/* this array will store number of arrangement that word can be stored */

var totalArrangements = [];

/* iterate through string user enter */

for (var i = 0; i < userStr.length; i++) {

/* get the current character */

var character = userStr[i];

/*check if character is already used than skip */

if (userStr.indexOf(character) != i)

continue;

/* create a string and concanete it form 0 to i index and i+1 to length of array */

var str = userStr.slice(0, i) + userStr.slice(i + 1, userStr.length);

/* push it to totalArrangments */

for (var subPermutation of combinations(str))

totalArrangements.push(character + subPermutation)

}

/* return the total arrangements */

return totalArrangements;

}

/* ask user to prompt string */

var userStr = prompt("Enter a string: ");

/* count to count the total number of combinations */

let count = 0;

/* call function the return tolal number of arrangement in array */

totalArrangements = combinations(userStr);

/* print the combination in console */

for (permutation of totalArrangements) {

console.log(permutation)

}

/* total combination will be totalArrangements.length */

count = totalArrangements.length;

/*print it to console */

console.log("Total combination " + count)

Output:-

1. Create a pseudocode program that asks students to enter a word. Call a function-example-1
User Helal
by
5.5k points