screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Random Item from Array</title> </head> <body> <h1>Return Random Element from Array Javascript</h1> <button onclick="showRandomItem()">Show Random Item</button> <p id="randomArrayDisplay"></p> <p id="randomItemDisplay"></p> <script> function getRandomItem(array) { const randomIndex = Math.floor(Math.random() * array.length); return array[randomIndex]; } function showRandomItem() { const myArray = [1, 2, 3, 4, 5]; const randomItem = getRandomItem(myArray); document.getElementById('randomArrayDisplay').textContent = `Array: [${myArray.join(', ')}]`; document.getElementById('randomItemDisplay').textContent = `Random Item: ${randomItem}`; } </script> </body> </html>