187k views
1 vote
Create an array of 10 fortune cookie sayings that will be randomly displayed each time the user reloads the page. The fortune will be sayings like: "Procrastination is the thief of time." Let the visitor get a new fortune when a button is clicked. g

User DIG
by
3.5k points

1 Answer

7 votes

Answer:

let cookieNumber = Math.floor(Math.random() * 10)

switch (cookieNumber) {

case 1:

document.write('Fortune 1')

break;

case 2:

document.write('Fortune 2')

break;

case 3:

document.write('Fortune 3')

break;

case 4:

document.write('Fortune 4')

break;

case 5:

document.write('Fortune 5')

break;

case 6:

document.write('Fortune 6')

break;

case 7:

document.write('Fortune 7')

break;

case 8:

document.write('Fortune 8')

break;

case 9:

document.write('Fortune 9')

break;

case 10:

document.write('Fortune 10')

Step-by-step explanation:

The cookieNumber is generated using Math.random(), which is rounded to a whole number using Math.floor(). Then, a switch block is used to display a different fortune depending on the value of cookieNumber.

User Knorv
by
3.0k points