Coding Practice in JavaScript: Alerts, Loops, Functions, and Strings
1. Question: Create a program that asks the user for two numbers using prompts. Perform the following operations and display the results using alerts:
Addition, Subtraction, Multiplication, Division
Display an error if non-numeric values are entered.
const num1 = prompt("Enter the first number:");
const num2 = prompt("Enter the second number:");
// Check if inputs are valid numbers
if (isNaN(num1) || isNaN(num2)) {
alert("Error: Please enter valid numeric values.");
} else {
const n1 = parseFloat(num1);
const n2 = parseFloat(num2);
alert(`Addition: ${n1 + n2}`);
alert(`Subtraction: ${n1 - n2}`);
alert(`Multiplication: ${n1 * n2}`);
alert(`Division: ${n1 / n2}`);
}
2. Question: Write a program that asks the user for a number. Use if...else to determine if the number is: Positive, Negative, Zero Additionally, check if the number is even or odd.
const num = parseInt(prompt("Enter a number:"));
if (num > 0) {
console.log("The number is positive.");
console.log(num % 2 === 0 ? "It is even." : "It is odd.");
} else if (num < 0) {
console.log("The number is negative.");
console.log(num % 2 === 0 ? "It is even." : "It is odd.");
} else {
console.log("The number is zero.");
}
3. Write a program that generates the multiplication table for a number entered by the user using a for loop. Print the results in the console.
const number = parseInt(prompt("Enter a number to generate its multiplication table:"));
console.log(`Multiplication Table for ${number}:`);
for (let i = 1; i <= 10; i++) {
console.log(`${number} x ${i} = ${number * i}`);
}
4. Question: Write a program that calculates the sum of all digits in a number entered by the user. Use a while loop.
let number = parseInt(prompt("Enter a number:"));
let sum = 0;
while (number > 0) {
sum += number % 10; // Add the last digit
number = Math.floor(number / 10); // Remove the last digit
}
console.log("The sum of the digits is:", sum);
5. Question: Write a program with a function isPrime(n) that checks if a number is prime. Use the function to check numbers entered by the user.
function isPrime(n) {
if (n <= 1) return false;
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i === 0) return false;
}
return true;
}
const number = parseInt(prompt("Enter a number to check if it is prime:"));
if (isPrime(number)) {
console.log(`${number} is a prime number.`);
} else {
console.log(`${number} is not a prime number.`);
}
6. Question: Write a program that takes a sentence from the user and counts the number of words in it. Ignore extra spaces.
const sentence = prompt("Enter a sentence:").trim();
// Split the sentence into words and filter out empty strings
const words = sentence.split(/\s+/);
console.log(`The number of words in the sentence is: ${words.length}`);
7. Question: Write a program that asks the user for a string and performs the following operations: Convert the string to lowercase, Reverse the string and Check if the string is a palindrome (reads the same forward and backward).
const input = prompt("Enter a string:").toLowerCase();
// Reverse the string
const reversed = input.split("").reverse().join("");
// Check if palindrome
if (input === reversed) {
console.log("The string is a palindrome.");
} else {
console.log("The string is not a palindrome.");
}