If you managed to do that in C++ you shouldn't have a problem recreating that logic in JavaScript.
Anyway, I assembled a little snippet for you:
var createWordFromPattern = function(pattern) {
var resultStack = [];
for(var i=0; i<pattern.length; i++) {
var sign = pattern.charAt(i);
var signResult = getRandomSubstituteForSign(sign);
if(signResult !== null) {
resultStack.push(signResult);
}
}
return resultStack.join("");
}
var getRandomSubstituteForSign = function(sign) {
var vowels = ['a', 'e', 'i', 'o', 'u'];
var consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l']; //just added some for demonstration
if(sign === 'c') {
return consonants[Math.floor(Math.random()*consonants.length)]
}
if(sign === 'v') {
return vowels[Math.floor(Math.random()*vowels.length)]
}
return null;
}
document.write(createWordFromPattern("cvvcv"));
You can press Run code snippet
a few times to see the results.
It's very verbose and missing many consonants, but I think you get the idea of how to tackle this problem.
As this stands, it will turn c
s in the pattern String to a random entry from the consonants array, and v
s to a random entry from the vowels array. Every other character in the pattern string will be ignored.
This can easily be expanded by adding detection for more signs for example, but this is just to give you an idea about how to solve the problem.