๐ Q1. How do you check if a number is even or odd?
let num = 10;
if (num % 2 === 0) {
console.log("Even");
} else {
console.log("Odd");
}
๐ Q2. How do you reverse a string?
let text = "hello";
let reversedText = text.split("").reverse().join("");
console.log(reversedText); // Output: olleh
๐ Q3. Write a function to find the factorial of a number.
function factorial(n) {
let result = 1;
for (let i = 1; i <= n; i++) {
result *= i;
}
return result;
}
console.log(factorial(5)); // Output: 120๐ Q4. How do you remove duplicates from an array?
let items = [1, 2, 2, 3, 4, 4];
let uniqueItems = [...new Set(items)];
console.log(uniqueItems);
๐ Q5. Print numbers from 1 to 10 using a loop.
for (let i = 1; i <= 10; i++) {
console.log(i);
}๐ Q6. Check if a word is a palindrome.
let word = "madam";
let reversed = word.split("").reverse().join("");
if (word === reversed) {
console.log("Palindrome");
} else {
console.log("Not a palindrome");
}
๐ฌ Tap โค๏ธ for more!