Random Ice Cream Facts Script (JavaScript)

This notebook includes a JavaScript script that outputs a random fact about ice cream from a predefined list of 10 facts. Below is a brief explanation of how the process works:

Array of Facts: We define an array called facts that stores 10 random facts about ice cream. Each fact is a string within this array.

Random Index Generation:

The getRandomFact function uses Math.random() to generate a random number between 0 and 1. We then multiply this number by the length of the facts array and use Math.floor() to round it down to the nearest whole number. This gives us a valid random index for the array. Displaying a Fact:

The function selects the fact at the randomly generated index and prints it to the console using console.log(). Function Call: Finally, we call the getRandomFact function to display one random fact each time the script is run.

%%javascript
// Array of 10 random facts about ice cream
const facts = [
    "Ice cream was first invented in China around 200 BC.",
    "The United States consumes the most ice cream in the world.",
    "Vanilla is the most popular ice cream flavor.",
    "It takes about 12 pounds of milk to make one gallon of ice cream.",
    "The waffle cone was invented in 1904 at the World's Fair.",
    "July is the national ice cream month in the United States.",
    "The most expensive ice cream sundae costs $25,000.",
    "The first ice cream parlor in the U.S. opened in New York City in 1790.",
    "Ice cream headaches occur when cold hits nerves in the mouth.",
    "New Zealand consumes more ice cream per capita than any other country."
];

function getRandomFact() {
    const randomIndex = Math.floor(Math.random() * facts.length);
    console.log("Random Ice Cream Fact: " + facts[randomIndex]);
}

getRandomFact();