0% found this document useful (0 votes)
9 views7 pages

Js Questions

The document contains a comprehensive list of JavaScript programming exercises covering various topics such as variables, data types, loops, operators, functions, and advanced concepts. Each section includes specific tasks, such as declaring variables, checking data types, implementing loops, and creating functions for real-world scenarios. It serves as a practical guide for beginners to practice and enhance their JavaScript skills.

Uploaded by

Dhanaraj Bhaskar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views7 pages

Js Questions

The document contains a comprehensive list of JavaScript programming exercises covering various topics such as variables, data types, loops, operators, functions, and advanced concepts. Each section includes specific tasks, such as declaring variables, checking data types, implementing loops, and creating functions for real-world scenarios. It serves as a practical guide for beginners to practice and enhance their JavaScript skills.

Uploaded by

Dhanaraj Bhaskar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Variables

1. Write a program to declare variables using var, let, and const. Try reassigning each
one and observe what happens.
2. Create a variable without initializing it and log its value. Explain what undefined
means in JavaScript.
3. Declare two variables and swap their values without using a third variable.
4. What will happen if you try to access a variable declared with let before
initialization? Write a code snippet to demonstrate this.
5. Declare a global variable and a block-scoped variable. Show how they differ in
accessibility within and outside a block.

Data Types

6. Write a program to check and log the type of the following values: 42, "Hello", true,
null, undefined, and {}.
7. Write a program to convert a number to a string and a string to a number.
8. Demonstrate implicit and explicit type conversion in JavaScript with examples.
9. Write a function that takes any value and checks if it is null or undefined.
10. Create a program that checks if a variable is of type object and if it is an array.

Loops

11. Write a for loop to print the numbers from 1 to 10.


12. Use a while loop to calculate the sum of the first 50 natural numbers.
13. Create a program to print all even numbers between 1 and 20 using a for loop.
14. Write a program that uses a do-while loop to repeatedly prompt the user for input
until they enter "exit".
15. Use a nested loop to generate the following pattern:

*
**
***
****
*****

Operators

16. Write a program to demonstrate the difference between == and === with examples.
17. Create a program to calculate the result of (10 + 5) * 2 - 3 using arithmetic operators.
18. Write a program to check if a number is positive, negative, or zero using conditional
(ternary) operators.

Cisco Confidential
19. Demonstrate the use of logical operators (&&, ||, !) in a program that checks if a
number is within a specific range.
20. Create a program to increment and decrement a variable using both ++ and --
operators.

Functions

21. Write a function to calculate the factorial of a given number.


22. Create a function that takes two numbers as arguments and returns their sum,
difference, product, and quotient.
23. Write an arrow function to check if a number is odd or even.
24. Write a program to demonstrate function hoisting in JavaScript.
25. Create a function that takes an array of numbers and returns the maximum value in
the array.

Scenarios and Real-World Use Cases

26. Write a function that validates if a given string is a valid email address.
27. Create a program that simulates a basic calculator. Use a function for each operation
(add, subtract, multiply, divide).
28. Write a program that takes an array of numbers and returns a new array with all
numbers doubled.
29. Create a function that takes a string and returns the string reversed.
30. Write a program to sort an array of numbers in ascending order.

Advanced Concepts for Beginners

31. Write a program that demonstrates variable shadowing.


32. Create a function using default parameters.
33. Write a program that uses the spread operator to merge two arrays.
34. Demonstrate the use of map(), filter(), and reduce() on an array of numbers.
35. Write a function that uses recursion to calculate the nth Fibonacci number.

Miscellaneous

36. Write a program that checks if a given number is a prime number.


37. Create a function to check if two strings are anagrams of each other.
38. Write a program that generates a random number between 1 and 100 and asks the
user to guess it.
39. Demonstrate the use of try-catch to handle errors in a program.

Cisco Confidential
40. Write a program to debounce a function call.
41. Create a function to find the frequency of characters in a string.
42. Write a program that flattens a nested array (e.g., [1, [2, [3, 4]]] to [1, 2, 3, 4]).
43. Create a function that returns all unique elements in an array.
44. Write a function to capitalize the first letter of each word in a string.
45. Create a program that finds the GCD (Greatest Common Divisor) of two numbers.
46. Write a program that uses the setTimeout function to log "Hello, World!" after 3
seconds.
47. Create a function that returns the intersection of two arrays.
48. Write a program to count the number of vowels in a given string.
49. Create a function that finds the first non-repeating character in a string.
50. Write a program that removes duplicate values from an array without using Set.

Cisco Confidential
JavaScript Snippet Practice Questions

Variables

1. Declare a variable using let and assign it a value. Log the value,
reassign it, and log the new value.
let message = "Hello, World!";
console.log(message);
message = "Hello, JavaScript!";
console.log(message);

2. Swap two numbers using a temporary variable.


let a = 5, b = 10;
let temp = a;
a = b;
b = temp;
console.log(a, b);

3. Demonstrate hoisting with var.


console.log(x);
var x = 10;

4. Use const to declare a variable and try reassigning it.


const pi = 3.14;
// pi = 3.15; // Uncomment to see the error
console.log(pi);

5. Show the difference between var, let, and const in a loop.


for (var i = 0; i < 3; i++) {
console.log(i);
}
console.log(i); // Accessible outside the loop

for (let j = 0; j < 3; j++) {


console.log(j);
}
// console.log(j); // Uncomment to see the error

Data Types

Cisco Confidential
6. Convert a number to a string and vice versa.
let num = 42;
let str = num.toString();
console.log(typeof str, str);

let numConverted = Number(str);


console.log(typeof numConverted, numConverted);

7. Check the type of a variable using typeof.


let values = [42, "Hello", true, null, undefined, {}];
values.forEach(value => console.log(typeof value, value));

8. Write a program to check if a value is an array.


let arr = [1, 2, 3];
console.log(Array.isArray(arr));

9. Check if a variable is null or undefined.


let value = null;
console.log(value === null || value === undefined);

10. Demonstrate implicit and explicit type conversion.


console.log("5" + 5); // Implicit
console.log(Number("5") + 5); // Explicit

Loops

11. Print numbers from 1 to 10 using a for loop.


for (let i = 1; i <= 10; i++) {
console.log(i);
}

12. Calculate the sum of numbers from 1 to 100 using a while


loop.

Cisco Confidential
let sum = 0;
let i = 1;
while (i <= 100) {
sum += i;
i++;
}
console.log(sum);

13. Print even numbers from 1 to 20 using a loop.


for (let i = 1; i <= 20; i++) {
if (i % 2 === 0) console.log(i);
}

14. Reverse an array using a loop.


let arr = [1, 2, 3, 4, 5];
let reversed = [];
for (let i = arr.length - 1; i >= 0; i--) {
reversed.push(arr[i]);
}
console.log(reversed);

15. Generate a pyramid pattern.


let rows = 5;
for (let i = 1; i <= rows; i++) {
console.log("*".repeat(i));
}

16. Find the largest number in an array using a loop.


let nums = [10, 20, 5, 40, 30];
let max = nums[0];
for (let num of nums) {
if (num > max) max = num;
}
console.log(max);

17. Create a new array with doubled values.


let numbers = [1, 2, 3, 4, 5];
let doubled = numbers.map(num => num * 2);
console.log(doubled);

18. Calculate the factorial of a number using a loop.


let n = 5;

Cisco Confidential
let factorial = 1;
for (let i = 1; i <= n; i++) {
factorial *= i;
}
console.log(factorial);

19. Use do-while to repeatedly prompt for input.


let input;
do {
input = prompt("Enter a value (type 'exit' to quit):");
} while (input !== "exit");
console.log("Exited");

20. Sum the elements of an array using a loop.


let nums = [1, 2, 3, 4, 5];
let sum = 0;
for (let num of nums) {
sum += num;
}
console.log(sum);

Cisco Confidential

You might also like