Assignment No.
02 Total Marks: 15
Fall 2024 Due Date: 06-Jan-2025
FAIQA SHAHZADI
CS406 – Web Based Database Applications (BC230209196)
Topics Covered:
Module 124 - 132
Problem Statement: Marks: 15
Questions No.1: (10 Marks)
Write a JavaScript program to solve the following tasks:
1. Ask the user to input a number.
2. Check whether the number is even or odd.
3. If the number is even, calculate and display its square. If it is odd, calculate and display its cube.
Solution:
// Ask the user to input a number
let number = parseInt(prompt("Enter a number:"));
// Check if the number is even or odd
if (number % 2 === 0) {
// If even, calculate and display its square
let square = number * number;
alert("The number is even. Its square is: " + square);
} else {
// If odd, calculate and display its cube
let cube = number * number * number;
alert("The number is odd. Its cube is: " + cube);
}
Questions No.2: (5 Marks)
Write a JavaScript program that performs the following steps:
1. Ask the user to input a string.
2. Count and display the number of vowels (a, e, i, o, u) in the string.
Solution:
// Ask the user to input a string
let inputString = prompt("Enter a string:");
// Define vowels
let vowels = "aeiouAEIOU";
// Initialize counter for vowels
let vowelCount = 0;
// Loop through the string and count vowels
for (let i = 0; i < inputString.length; i++) {
if (vowels.includes(inputString[i])) {
vowelCount++;
}
}
// Display the result
alert("The number of vowels in the string is: " + vowelCount);
Deadline: Your assignment must be uploaded on or before 06 January 2025. No solution will be accepted
through email after the due date.