adding a feature to select the number of vowels and consonants in a row

This commit is contained in:
bernd32 2022-04-16 18:17:25 +05:00
parent a90aef46f2
commit 74da9707bc
2 changed files with 21 additions and 9 deletions

View File

@ -60,6 +60,12 @@
<span>Number of items: </span> <span>Number of items: </span>
<input id="number-of-items" type="number" value="1" min="1" max="100" class="number-input"> <input id="number-of-items" type="number" value="1" min="1" max="100" class="number-input">
<br><br> <br><br>
<span>Max. consonants in row: </span>
<input id="consonants-in-row" type="number" value="2" min="0" max="10" class="number-input">
<br><br>
<span>Max. vowels in row: </span>
<input id="vowels-in-row" type="number" value="2" min="0" max="10" class="number-input">
<br><br>
<button type="submit">Generate</button> <button type="submit">Generate</button>
</form> </form>
<ul id="result-list"></ul> <ul id="result-list"></ul>

View File

@ -3,13 +3,16 @@ const lengthMax = document.getElementById('length-max');
const lenghtMin = document.getElementById('length-min'); const lenghtMin = document.getElementById('length-min');
const numberOfItems = document.getElementById('number-of-items'); const numberOfItems = document.getElementById('number-of-items');
const resultList = document.getElementById('result-list'); // <ul></ul> const resultList = document.getElementById('result-list'); // <ul></ul>
const maxConsonantsInRow = document.getElementById('consonants-in-row');
const maxVowelsInRow = document.getElementById('vowels-in-row');
form.addEventListener('submit', event => { form.addEventListener('submit', event => {
event.preventDefault(); event.preventDefault();
resultList.innerHTML = ''; resultList.innerHTML = '';
console.log(length); console.log(length);
const wordList = getArrayWithRandomWords( const wordList = getArrayWithRandomWords(
numberOfItems.value, lenghtMin.value, lengthMax.value numberOfItems.value, lenghtMin.value, lengthMax.value,
maxConsonantsInRow.value, maxVowelsInRow.value
); );
for (let i = 0; i < wordList.length; i++) { for (let i = 0; i < wordList.length; i++) {
resultList.insertAdjacentHTML('afterbegin', `<li>${wordList[i]}</li>`); resultList.insertAdjacentHTML('afterbegin', `<li>${wordList[i]}</li>`);
@ -60,8 +63,8 @@ const vowelsWithWeights = {
* @returns {{item: any, index: number}} * @returns {{item: any, index: number}}
*/ */
for(let i=0; i<10; i++) { for (let i = 0; i < 10; i++) {
console.log(Math.random()*11); console.log(Math.random() * 11);
} }
@ -107,7 +110,7 @@ function addVowel() {
return weightedRandom(letters, weights); return weightedRandom(letters, weights);
} }
function getRandomWord(len = 5, consonantsInWord = 0.5) { function getRandomWord(len = 5, consonantsInWord = 0.5, maxConsonantsInRow = 2, maxVowelsInRow = 2) {
let wordGen = ''; let wordGen = '';
let skipConsotant = cons => Math.random() > cons ? true : false; let skipConsotant = cons => Math.random() > cons ? true : false;
let isDigram = letter => letter.length == 2 ? true : false; let isDigram = letter => letter.length == 2 ? true : false;
@ -119,7 +122,7 @@ function getRandomWord(len = 5, consonantsInWord = 0.5) {
if (skipConsotant(consonantsInWord)) { if (skipConsotant(consonantsInWord)) {
const vowel = addVowel(); const vowel = addVowel();
// skipping a vowel if we already have 2 vowels in row // skipping a vowel if we already have 2 vowels in row
if (vowelCounter >= 2) { if (vowelCounter >= maxVowelsInRow) {
i -= 1; i -= 1;
continue; continue;
} else { } else {
@ -138,7 +141,7 @@ function getRandomWord(len = 5, consonantsInWord = 0.5) {
continue; continue;
} }
// skipping a consonant if we already have 2 consonants in row // skipping a consonant if we already have 2 consonants in row
if (consonantCounter >= 2) { if (consonantCounter >= maxConsonantsInRow) {
i -= 1; i -= 1;
continue; continue;
} else { } else {
@ -159,11 +162,14 @@ function getRandomWord(len = 5, consonantsInWord = 0.5) {
* @param {number} n number of words to generate * @param {number} n number of words to generate
* @param {number} min minimum length of a word * @param {number} min minimum length of a word
* @param {number} max maximum length of a word * @param {number} max maximum length of a word
* @param {number} maxConsonantsInRow
* @param {number} maxVowelsInRow
*/ */
function getArrayWithRandomWords(n = 10, min = 3, max = 7) { function getArrayWithRandomWords(n = 10, min = 3, max = 7,
maxConsonantsInRow = 2, maxVowelsInRow = 2) {
let words = []; let words = [];
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
words.push(getRandomWord(getRandomInt(min, max), 0.6)); words.push(getRandomWord(getRandomInt(min, max), 0.6, maxConsonantsInRow, maxVowelsInRow));
} }
return words; return words;
} }