Advertisement
brandblox

WebLab(24/9/24)

Sep 24th, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>JavaScript Pop-Up Boxes</title>
  7. <style>
  8. body {
  9. font-family: Arial, sans-serif;
  10. margin: 20px;
  11. }
  12. button {
  13. margin: 5px;
  14. padding: 10px 15px;
  15. font-size: 16px;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <h1>JavaScript Pop-Up Box Examples</h1>
  21.  
  22. <button onclick="displayDate()">Display Date</button>
  23. <input type="text" id="dateOutput" readonly />
  24.  
  25. <button onclick="calculateFactorial()">Calculate Factorial</button>
  26.  
  27. <button onclick="multiplicationTable()">Multiplication Table</button>
  28.  
  29. <button onclick="sumOfNumbers()">Sum of Numbers</button>
  30.  
  31. <script src="script.js"></script>
  32. </body>
  33. </html>
  34.  
  35.  
  36.  
  37. function displayDate() {
  38. const currentDate = new Date().toLocaleDateString();
  39. document.getElementById('dateOutput').value = currentDate;
  40. }
  41.  
  42. function calculateFactorial() {
  43. let n = prompt("Enter a number to calculate its factorial:");
  44. n = parseInt(n);
  45. if (isNaN(n) || n < 0) {
  46. alert("Please enter a non-negative integer.");
  47. return;
  48. }
  49. let factorial = 1;
  50. for (let i = 1; i <= n; i++) {
  51. factorial *= i;
  52. }
  53. alert(`The factorial of ${n} is ${factorial}.`);
  54. }
  55.  
  56. function multiplicationTable() {
  57. let n = prompt("Enter a number to generate its multiplication table:");
  58. n = parseInt(n);
  59. if (isNaN(n)) {
  60. alert("Please enter a valid number.");
  61. return;
  62. }
  63. let table = "";
  64. for (let i = 1; i <= 10; i++) {
  65. table += `${n} x ${i} = ${n * i}\n`;
  66. }
  67. alert(`Multiplication table for ${n}:\n${table}`);
  68. }
  69.  
  70. function sumOfNumbers() {
  71. let n = prompt("Enter the total number of terms to sum:");
  72. n = parseInt(n);
  73. if (isNaN(n) || n <= 0) {
  74. alert("Please enter a positive integer.");
  75. return;
  76. }
  77.  
  78. let sum = 0;
  79. for (let i = 1; i <= n; i++) {
  80. let nextNum = prompt(`Enter number ${i}:`);
  81. nextNum = parseFloat(nextNum);
  82. if (isNaN(nextNum) || nextNum < 0) {
  83. alert("Please enter a valid number.");
  84. i--;
  85. } else {
  86. sum += nextNum;
  87. }
  88. }
  89. alert(`The sum of the ${n} numbers is ${sum}.`);
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement