lundi 5 juillet 2021

Struggling to get a random name generator to work

Trying to get this below to generate a random name made up of two parts and pushed together, I need help making it so that it prints out to the screen and so that it will chose one of the three options every time as at the moment sometimes it chooses none. I'm new to JavaScript.

const randomName = {
   prefix: ['Bramble', 'Lion', 'Owl'],
   suffix: ['Paw', 'Throat', 'Tail']
};

let firstHalf = () => {
switch(Math.floor(Math.random() * 10)){
  case 0:
      console.log(randomName.prefix[0])
  break;
  case 1:
      console.log(randomName.prefix[1])
  break;
  case 2:
      console.log(randomName.prefix[2])
  break;
};
};

let secondHalf = () => {
switch(Math.floor(Math.random() * 10)){
    case 0:
        console.log(randomName.suffix[0])
    break;
    case 1:
        console.log(randomName.suffix[1])
    break;
    case 2:
        console.log(randomName.suffix[2])
    break;
  };
};


console.log(`Your warrior name is ${firstHalf()} ${secondHalf()}!`);



Aucun commentaire:

Enregistrer un commentaire