Button - random selection

Viewed 20

I'm creating a button that will ask 'What is your first name?', then 'Would you like to know your Hogwarts house?' and if the answer is yes, it will say 'name, your Hogwarts house is...'.

I don't know how to add a random selection of the 4 Hogwarts houses.

My HTML is: Sorting Hat My code is:enter image description here

1 Answers

You can save the 4 possible houses on an array. Then you can select a random item from the array.

Here is a little code example that might help you...

const houses = ["A", "B", "C", "D"];
const randomHouse = houses[Math.floor(Math.random() * houses.length)];
console.log(randomHouse);

Related