Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>JavaScript Pop-Up Boxes</title>
- <style>
- body {
- font-family: Arial, sans-serif;
- margin: 20px;
- }
- button {
- margin: 5px;
- padding: 10px 15px;
- font-size: 16px;
- }
- </style>
- </head>
- <body>
- <h1>JavaScript Pop-Up Box Examples</h1>
- <button onclick="displayDate()">Display Date</button>
- <input type="text" id="dateOutput" readonly />
- <button onclick="calculateFactorial()">Calculate Factorial</button>
- <button onclick="multiplicationTable()">Multiplication Table</button>
- <button onclick="sumOfNumbers()">Sum of Numbers</button>
- <script src="script.js"></script>
- </body>
- </html>
- function displayDate() {
- const currentDate = new Date().toLocaleDateString();
- document.getElementById('dateOutput').value = currentDate;
- }
- function calculateFactorial() {
- let n = prompt("Enter a number to calculate its factorial:");
- n = parseInt(n);
- if (isNaN(n) || n < 0) {
- alert("Please enter a non-negative integer.");
- return;
- }
- let factorial = 1;
- for (let i = 1; i <= n; i++) {
- factorial *= i;
- }
- alert(`The factorial of ${n} is ${factorial}.`);
- }
- function multiplicationTable() {
- let n = prompt("Enter a number to generate its multiplication table:");
- n = parseInt(n);
- if (isNaN(n)) {
- alert("Please enter a valid number.");
- return;
- }
- let table = "";
- for (let i = 1; i <= 10; i++) {
- table += `${n} x ${i} = ${n * i}\n`;
- }
- alert(`Multiplication table for ${n}:\n${table}`);
- }
- function sumOfNumbers() {
- let n = prompt("Enter the total number of terms to sum:");
- n = parseInt(n);
- if (isNaN(n) || n <= 0) {
- alert("Please enter a positive integer.");
- return;
- }
- let sum = 0;
- for (let i = 1; i <= n; i++) {
- let nextNum = prompt(`Enter number ${i}:`);
- nextNum = parseFloat(nextNum);
- if (isNaN(nextNum) || nextNum < 0) {
- alert("Please enter a valid number.");
- i--;
- } else {
- sum += nextNum;
- }
- }
- alert(`The sum of the ${n} numbers is ${sum}.`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement