I’m pretty new at JavaScript. I just finished a JavaScript course so I’m working on some projects I came up with myself. Also any advice outside of the question im asking is welcomed too. So I’m trying to generate multiple different random letter phrases ranging from 1 letter to 6 letters with the object but I cant figure out how to generate several. Im only able to do one phrase, Is there a way to do this, Im thinking with some kind of loop? Not sure, thanks
const Words = {
0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e',
5: 'f', 6: 'g', 7: 'h', 8: 'i', 9: 'j',
10: 'k', 11: 'l', 12: 'm', 13: 'n', 14: 'o',
15: 'p', 16: 'q', 17: 'r', 18: 's', 19: 't',
20: 'u', 21: 'v', 22: 'w', 23: 'x', 24: 'y', 25: 'z'
}
const phraseLength = Math.floor(Math.random() * (5 - 1 + 1)) + 1
const firstWord = Words[Object.keys(Words)[Math.floor(Math.random() * Object.keys(Words).length)]]
const secondWord = Words[Object.keys(Words)[Math.floor(Math.random() * Object.keys(Words).length)]]
const thirdWord = Words[Object.keys(Words)[Math.floor(Math.random() * Object.keys(Words).length)]]
const fourthWord = Words[Object.keys(Words)[Math.floor(Math.random() * Object.keys(Words).length)]]
const fifthWord = Words[Object.keys(Words)[Math.floor(Math.random() * Object.keys(Words).length)]]
const sixthWord = Words[Object.keys(Words)[Math.floor(Math.random() * Object.keys(Words).length)]]
const numberstudents = () => {}
const name = nameLength => {
if (phraseLength === 1) {
return firstWord
} else if (phraseLength === 2) {
return firstWord + secondWord
} else if (phraseLength === 3) {
return firstWord + secondWord + thirdWord
} else if (phraseLength === 4) {
return firstWord + secondWord + thirdWord + fourthWord
} else if (phraseLength === 5) {
return firstWord + secondWord + thirdWord + fourthWord + fifthWord
} else if (phraseLength === 6) {
firstWord + secondWord + thirdWord + fourthWord + fifthWord + sixthWord
}
}
console.log(name(phraseLength))
Im fairly new at coding and this is my first time asking for any any kind of help on the internet. So once again thanks
3
Answers
There are a lot of ways you could do this, but a simple method (if you’re familiar with arrays) would be: