0

Trying to make a word generator with Javascript, please help.

I'm trying to create a word generator with javascript, can someone help me write the code and improve the lines of code I already put in. Here's the code: var numberOfWords = parseInt(prompt("How many words would you like? ")); var letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",]; var i; var x; var allWords = []; var word = []; // Always clear this array after creating a word var newWord; // Keep creating words until the desired number is reached for (x = 0; x < numberOfWords; x += 1) { // Pick a random number to be length of the word var lengthOfWord = Math.floor(Math.random() * 10) + 1; // returns a random integer from 1 to 10 // start creating a word for (i = 0; i < lengthOfWord; i += 1) { // Select a letter at random from the array "letters" var selectedLetter = letters[Math.floor(Math.random() * letters.length)]; // Put the randomly selected letter inside an array word.push(selectedLetter); // if the lenght of the words has been reached if (i === lengthOfWord) { // Join all the letters from the array together to form a word var newWord = word.join(""); // Put the word inside an array allWords.push(newWord); // Clear the array "word" word = []; } } } // Print all the words from the array "allWords" side by side with a space in between.

8th Dec 2020, 7:03 PM
Eternal Master
Eternal Master - avatar
1 Respuesta
0
This is a working version of your code. It works perfectly in chrome, if you have issues on phone, change the let and const keywords to var. Hope this helps. https://code.sololearn.com/Wa13A3a78a2a
9th Dec 2020, 8:33 AM
CHMD
CHMD - avatar