skip to Main Content

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


  1. 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 generateRandomPhrase = length => {
      let phrase = '';
      for (let i = 0; i < length; i++) {
        const randomIndex = Math.floor(Math.random() * Object.keys(Words).length);
        phrase += Words[randomIndex];
      }
      return phrase;
    };
    
    const numberstudents = () => {
      const numberOfPhrases = 5; // Change this to the desired number of phrases
      for (let i = 0; i < numberOfPhrases; i++) {
        const phraseLength = Math.floor(Math.random() * (6 - 1 + 1)) + 1;
        const randomPhrase = generateRandomPhrase(phraseLength);
        console.log(randomPhrase);
      }
    };
    
    numberstudents();
    
    Login or Signup to reply.
  2. There are a lot of ways you could do this, but a simple method (if you’re familiar with arrays) would be:

    // This is just a shortcut to generating an array of a-z
    const alphabet = Array.from({ length: 26 }, (_, i) =>
      String.fromCharCode(97 + i))
    
    const phraseLength = Math.floor(Math.random() * (5 - 1 + 1)) + 1
    
    const name = (nameLength) =>
      // Make an array with the length specified in the arg
      Array.from({ length: phraseLength }, (_) =>
        // Select letters at random from the array
        alphabet[Math.floor(Math.random() * alphabet.length)])
        // Join the resultant array to a string
        .join('')
    
    console.log(name(phraseLength))
    
    Login or Signup to reply.
  3. /*
    Naming:
    Changed the name from "Words" to letters as that is really what they 
    are. Naming things is one of the hardest things to do, but good names 
    allow us to think and reason more clearly.
    
    Data type choice:
    Use an Array rather than an Object, as Arrays already allow us to 
    access a property by index. Also makes code cleaner and faster, with 
    less Object.key() calls.
    */
    const 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"
    ];
    
    /*
    Encapsulation:
    Use functions whenever you have operations that run repeatedly. We need 
    to get a random number both to get the length of a phrase, and to pick
    a random letter. We'll put that in a function, and call that function
    instead. This allows us to run an operation repeatedly without hard 
    coding it.
    */
    function getRandomNumber(min, max) {
      return Math.floor(Math.random() * max) + min;
    }
    
    // The word generator could also be put in a function
    function getRandomWords(words) {
      for (let i = 0; i < words; i++) {
        const wordLength = getRandomNumber(1, 6);
        let word = "";
        for (let j = 0; j < wordLength; j++) {
          word += letters[getRandomNumber(0, 26)];
        }
        console.log(word);
      }
    }
    
    getRandomWords(5); //logs 5 random words
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search