I'm trying to make program in javascript that makes words of given letters (e.g. given letters: "a,b,c", words: "aaaa", "aaab"... and so on.
const random = (length = 6) => {
// Declare all characters
let chars = 'aąbcćdeęfghijklłmnńoópqrsśtuvwxyzźżAĄBCĆDEĘFGHIJKLŁMNŃOÓPQRSŚTUVWXYZŹŻ';
// Pick characters randomly
let str = '';
for (let i = 0; i < length; i ) {
str = chars.charAt(i);
}
return str;
};
Is there any way to do it? Thanks in advance
CodePudding user response:
If what you're looking for is every possible combination of letters, randomness isn't the way to do this.
Try taking a more methodical approach
combinations = []
function allPossibleCombinations(lettersRemaining, lettersSoFar){
if (lettersRemaining.length == 0){
combinations.push(lettersSoFar);
}else{
for (var i = 0; i < lettersRemaining.length; i ){
allPossibleCombinations(lettersSoFar lettersRemaining.charAt(i), lettersRemaining.slice(0, i) lettersRemaining.slice(i 1))
}
}
}
The code is untested, but this general recursive approach should work
CodePudding user response:
You should not be asking how to do it. As a programmer, it is your duty to solve the problem. Stack overflow is a place where you can ask your queries like I have been trying to do that but it's not working or showing abnormal results etc or something you can't find on google. you haven't said anything like you have tried this or that. please be more responsible and try to first solve it yourself. A programmer is first a problem solver.
Let me give you some hints every character has a unique code called charCode (google it please for more info) the charCode increases from a to z, you should be using these codes to start from the lower alphabet and move towards the higher charCode alphabet. i have given you enough material to start with google it and solve it yourself