JavaScript Practice Questions assignment
Part 1: Conditions (if, if-else, else-if, ternary) – 10 Questions
1. Check if a number is positive
Write a program that takes a number and checks if it is greater than 0 using an if statement.
let num = 10;
if (num > 0) {
[Link]("The number is positive.");
2. Check if a number is even or odd
Use the modulus operator (%) and if-else to check whether a number is even (divisible by 2) or odd.
let num = 7;
if (num % 2 === 0) {
[Link]("Even");
} else {
[Link]("Odd");
3. Find the greater of two numbers
Accept two numbers. Use if-else to compare and print the larger one.
4. Check if a person is eligible to vote
A person can vote if their age is 18 or above. Write a program that checks eligibility based on the
age.
5. Check if a number is divisible by 5 and 11
A number is divisible by both if it gives a remainder 0 for both 5 and 11.
if (num % 5 === 0 && num % 11 === 0) {
[Link]("Divisible by 5 and 11");
} else {
[Link]("Not divisible by 5 and 11");
6. Assign grade using else-if
Given a score, assign grades as follows:
• A: 90 and above
• B: 80–89
• C: 70–79
• D: 60–69
• F: Below 60
Use else if ladder.
7. Check if a character is a vowel or consonant
Input a single alphabet and check if it is one of a, e, i, o, u (vowel). Otherwise, it's a consonant.
8. Use ternary to check if number > 100
Use a ternary operator to print "Big" if number > 100, otherwise "Small".
let result = num > 100 ? "Big" : "Small";
9. Find the largest among 3 numbers using nested if
Accept 3 numbers and use if inside another if to compare and find the largest.
10. Check divisibility by 3 using ternary
Use ternary operator to check if a number is divisible by 3 and print the result.
Part 2: Loops with Conditions (20 Questions)
11. Print numbers 1 to 10
Use a for loop to print numbers from 1 to 10.
12. Print even numbers 1 to 50
Use loop + if to check if each number is divisible by 2, then print.
13. Print numbers divisible by 3 from 1 to 100
Loop through numbers 1–100 and print only if divisible by 3.
14. Count numbers divisible by 7 between 1–100
Use a counter to track how many numbers in 1–100 are divisible by 7.
15. Sum of odd numbers from 1 to 50
Use loop + if (num % 2 !== 0) to add only odd numbers.
16. Print square of numbers 1 to 10
Use for loop and print each number’s square using [Link]() or n * n.
17. Print only positive numbers from an array
Given an array, loop through it and use if to print only numbers > 0.
let arr = [2, -5, 6, -1, 3, -9];
18. Count numbers > 10 in array
Count how many elements in the array are greater than 10.
19. FizzBuzz (classic problem)
For numbers 1–30:
• Print Fizz if divisible by 3
• Print Buzz if divisible by 5
• Print FizzBuzz if divisible by both
20. Check if a number is prime
A prime number is only divisible by 1 and itself. Loop from 2 to n - 1 and check if divisible by any
number.
21. Sum of digits of a number
Given a number like 123, break it into digits and sum them: 1 + 2 + 3 = 6.
22. Reverse a number
Use loop to extract digits from the end and build the reverse. (e.g., 123 → 321)
23. Factorial of a number
Factorial of n is n * (n-1) * ... * 1. Use a loop to calculate factorial.
24. Print first 10 multiples of a number
Input a number and print its first 10 multiples using a loop.
25. Count vowels in a string
Loop through a string and count how many characters are vowels (a, e, i, o, u).
26. Print even numbers from array
Given an array, print only the even values.
27. Find largest number in array
Loop through the array, compare and track the largest number.
28. Print “Pass” or “Fail” using ternary
For each mark in an array, use ternary:
mark >= 40 ? "Pass" : "Fail"
29. Count negative numbers in an array
Use loop + if (num < 0) to count how many elements are negative.
30. Print numbers divisible by 3 but not 9
Loop through array or range. Use:
if (num % 3 === 0 && num % 9 !== 0)