Write a program that displays, validates, and processes a form intended for user registration. User registration captures the following details: username, password, gender (Male or Female), marital status (Single or Married), and preferences (Music, football, netball, volleyball). The form should be validated to prevent submission of empty fields.
a) Write a JavaScript function, which receives an array of numbers as an argument and returns the product of the numbers in the array.
Answer (Click to show)
function productOfArray(numbers) { let product = 1; for (let i = 0; i < numbers.length; i++) { product *= numbers[i]; } return product;}
b) Use the JavaScript Document Object Model (DOM) to create a paragraph and add it to the HTML document body. The paragraph displays the message “Welcome to programming”.
Answer (Click to show)
const paragraph = document.createElement('p');paragraph.innerHTML = 'Welcome to programming';document.body.appendChild(paragraph);
c) Write a JavaScript function that deletes an HTML element with id=“item” from the document when an HTML button with id=“button” is clicked.
d) Write a JavaScript function that receives two numbers from a user through Browser Object Model (BOM) popups and displays the summation of the numbers through BOM popups.
Answer (Click to show)
function sumTwoNumbers() { const num1 = parseFloat(prompt('Enter the first number:')); const num2 = parseFloat(prompt('Enter the second number:')); alert('The sum is: '+ (num1 + num2));}