Issue
I am making a word game that picks 8 random letters from the alphabet and the player must find words that use these letters. I must find out a way to make the letters picked always contain 3 vowels and 5 consonants. I would appreciate your help
Here is the code I am using to pick the random letters for the game
function makeid(length) {
var result = '';
var characters = 'aaabcdeeefghiiijklmnooopqrstuuuvwxyz';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
let letter = characters.charAt(Math.floor(Math.random() * charactersLength));
while (result.match(letter)) {
letter = characters.charAt(Math.floor(Math.random() * charactersLength));
}
result += letter;
}
return result;
}
console.log("Id of 8 characters", makeid(8))
Solution
@Lars answer is almost perfect, the only problem is that the final string is not truly shuffled.
I suggest you just create two arrays: random vowels and random consonants and then just shuffle them using Fisher-Yates' algorithm.
function getRandomCharsFromString(str, length) {
return Array.from({length}, () => {
return str[Math.floor(Math.random() * str.length)]
});
}
function shuffle(str) {
var a = str.split(""),
n = a.length;
for (var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
// Swap them with ES6 destructuring magic
[a[i], a[j]] = [a[j], a[i]];
}
return a.join("");
}
function getRandomString() {
const vowels = 'aeiou';
const consonants = 'bcdfghjklmnpqrstvwxyz';
const randomVowels = getRandomCharsFromString(vowels, 3);
const randomConsonants = getRandomCharsFromString(consonants, 5);
const randomWord = [...randomVowels, ...randomConsonants].join('')
return shuffle(randomWord)
}
console.log('random string:', getRandomString())
No repeated letters
You mentioned you don't want repeated letters; many words in English have duplicate letters. Why is that a requirement?
You can shuffle the vowels
and consonants
and get the first x
characters from that string.
// This version makes sure characters are not repeated
function getRandomCharsFromString(str, length) {
return shuffle(str).slice(0, length);
}
function shuffle(str) {
var a = str.split(''),
n = a.length;
for (var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
// Swap them with ES6 destructuring magic
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
function getRandomString() {
const vowels = 'aeiou';
const consonants = 'bcdfghjklmnpqrstvwxyz';
const randomVowels = getRandomCharsFromString(vowels, 3);
const randomConsonants = getRandomCharsFromString(consonants, 5);
const randomWord = [...randomVowels, ...randomConsonants].join('')
return shuffle(randomWord).join('')
}
console.log('random string:', getRandomString())
Answered By - Juan Mendes
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.