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>Array Sum Example</title> </head> <body> <h1>Javascript Sum Array Values</h1> <!-- Original array and result will be displayed in these elements --> <p id="originalArray"></p> <p id="result"></p> <script> // JavaScript code document.addEventListener('DOMContentLoaded', function () { const numbers = [1, 2, 3, 4, 5]; // Display the original array const originalArrayElement = document.getElementById('originalArray'); originalArrayElement.textContent = `Original Array: [${numbers.join(', ')}]`; // Using reduce to calculate the sum const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0); // Display the result in the element with the 'result' ID const resultElement = document.getElementById('result'); resultElement.textContent = `Sum of array elements: ${sum}`; }); </script> </body> </html>