I. Solve the following problems by writing JavaScript code.
1. Write a JS program that accept a positive integer from the user and print all numbers
from 1 up to that number.
2. Write a JS program that ask the user for a temperature in Celsius and convert it to
Fahrenheit using the formula: F = C * 9/5 + 32.
3. Write a while loop that prints numbers from 5 down to 1. Ensure it doesn't create an
infinite loop.
4. Accept a number N from the user. Calculate and log the sum of all numbers from 1 to N
(e.g., if N = 4, result is 1 + 2 + 3 + 4 = 10).
5. Accept a number from the user and calculate its factorial (e.g., 5! = 5 * 4 * 3 * 2 * 1 =
120).
6. Write a loop that counts how many even numbers exist between 1 and 50, then prints the
final count.
7. Create a fixed password variable (e.g., "secret123"). Use a loop to repeatedly prompt the
user for the password until they type it correctly.
8. Prompt the user for a number N, then print the first N numbers of the Fibonacci sequence
(where each number is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8...).
9. Write a JavaScript program to find the larger of two numbers entered by the user.
10. Write a JavaScript program to calculate the area of a circle.
11. Write a JavaScript program to count the number of vowels in a string.
12. Write a JavaScript program to swap two numbers using a third variable.
13. Loop through numbers from 1 to 20. If a number is divisible by 3, print "Fizz". If it's
divisible by 5, print "Buzz". If it's divisible by both, print "FizzBuzz". Otherwise, just
print the number.
14. Accept a numerical score (0-100) from a user and log their letter grade based on standard
scales: 90+ is "A", 80-89 is "B", 70-79 is "C", 60-69 is "D", and below 60 is "F"
15. Write a JavaScript program to calculate the average of three numbers.
II. Write the output of the following snippet of JS programs.
1.
Program Output
let score = '100';
if (score === 100) {
[Link]('Perfect!');
} else {
[Link]('Try again!');
}
2.
Program Output
let a = 5;
let b = a++;
[Link](a, b);
3.
Program Output
let name = "";
let user = name || "Guest";
[Link](user);
4.
Program Output
if (true) {
var varNum = 10;
let letNum = 20;
}
[Link](varNum);
[Link](letNum);
5.
Program Output
[Link]("5" + 2);
[Link]("5" - 2);
6.
Program Output
let fruit = "apple";
switch (fruit) {
case "apple":
[Link]("Red");
case "banana":
[Link]("Yellow");
break;
default:
[Link]("Green");
}
7.
Program Output
let sum = 0;
for (let i = 1; i <= 5; i++) {
if (i % 2 === 0) continue;
sum += i;
}
[Link](sum);
8.
Program Output
let total = 0;
for (let i = 0; i < 2; i++) {
for (let j = 0; j < 3; j++) {
if (j === 1) break;
total += i;
}
}
[Link](total);
9.
Program Output
let x = 10;
if (true) {
let x = 20;
x += 5;
}
[Link](x);
10.
Program Output
let num = 5;
[Link](num ** 2);
11.
Program Output
let i = 5;
do {
i += 2;
} while (i < 5);
[Link](i);
12.
Program Output
let num = 0;
if (num === "0") {
[Link]("A");
} else if (num == "0") {
[Link]("B");
} else {
[Link]("C");
}
13.
Program Output
let text = "2";
do {
text += 1;
} while ([Link] < 3);
[Link](text);
14.
Program Output
let totalSum = 0;
let step = 0;
do {
let stepMultiplier = 2;
totalSum += step * stepMultiplier;
step++;
} while (step < 3);
[Link](totalSum);
15.
Program Output
let i = 0;
let result = 0;
do {
let j = 0;
while (j < 3) {
if (j === 1) break;
result += i;
j++;
}
i++;
} while (i < 2);
[Link](result);