Pick a random food from a list and display it with an image.
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<h2 id="result">今天中午吃什麼?</h2>
<button onclick="pickFood()">抽籤!</button>
<script>
function pickFood() {
let foods = [
{name: "便當", img: "https://pic.pngsucai.com/00/87/05/9e1c2cdce2519ccc.webp"},
{name: "炒飯", img: "https://pic.pngsucai.com/01/01/22/b8c1752bff44f5d1.webp"},
{name: "牛肉麵", img: "https://pic.pngsucai.com/00/97/95/35f4d876b11ed68f.webp"}
];
let randIndex = Math.floor(Math.random() * foods.length);
let choice = foods[randIndex];
document.getElementById("result").innerHTML =
choice.name + "<br><img src='" + choice.img + "' width='150'>";
}
</script>
</body>
</html>