JavaScript(40%) Tasks 9th Jan 2025
JavaScript(40%) Tasks 9th Jan 2025
3. Hoisting Test:
Write a program that demonstrates the difference between var , let , and
const hoisting behavior. Explain the results.
4. Immutable vs Mutable:
Declare a const object and try modifying its properties. Explain why the
object properties can be changed but the reference cannot.
JavaScript Task’s 1
5. Variable Swap (without temp):
Write a program to swap the values of two variables without using a third
variable or a library function.
2. Bitwise Maze:
Use bitwise operators ( | , & , ^ , << , >> ) to solve a problem, such as
finding if a number is even or odd, or swapping two numbers without using
arithmetic operators.
3. Chained Comparisons:
Write a program to evaluate a complex logical condition, such as (a > b && b
5. Arithmetic Series:
Using only the arithmetic operators, create a function to generate the sum
of the first n natural numbers (without using a loop).
56 to 56 ).
Write a function to simulate the behavior of a logical XOR using only logical
operators ( && , || , ! ).
JavaScript Task’s 2
Using the logical OR ( || ) operator, create a program that assigns a default
value to a variable if it is null or undefined .
5. Comparison Puzzle:
Write a program to test equality and strict equality ( == and === ) between
variables of different types. Explain when they produce the same result and
when they differ.
Create a function that takes a number and uses all compound assignment
operators ( += , -= , *= , /= , %= ) to produce a sequence of results.
8. Custom Increment/Decrement:
Implement your own version of the increment ( ++ ) and decrement ( -- )
operators without using them.
9. Unary Puzzle:
Use the unary plus ( + ) operator to convert strings into numbers and write a
program that shows its behavior with valid and invalid inputs.
Data Type’s
JavaScript Task’s 3
Write a function checkType that takes an input and logs its data type using
the typeof operator. Test with all primitive types (string, number, boolean,
null, undefined, bigint, and symbol).
2. BigInt Arithmetic:
Perform arithmetic operations (addition, subtraction, multiplication) on two
BigInt values. Demonstrate what happens when you mix BigInt and number
types.
3. Symbol Uniqueness:
Create two Symbol variables with the same description. Check if they are
equal and explain why they are not.
4. Boolean Conversion:
Write a function that converts different data types (e.g., strings, numbers,
objects) into boolean values. Test it with edge cases like 0 , NaN , and empty
objects.
Write a program to explain the difference between null and undefined . Use
examples to show how they behave in equality ( == ) and strict equality
( === ) comparisons.
3. Array Manipulation:
Declare an array with mixed data types (string, number, boolean). Write a
program to filter the array and group elements by their type.
4. Map vs Object:
JavaScript Task’s 4
each.
5. Set Uniqueness:
Create a Set and demonstrate how it handles duplicate values. Write a
function to convert an array with duplicates into a unique array using Set .
Create an array of all possible JavaScript falsy values and a separate array
of truthy values. Write a program to test their behavior in if conditions.
Create a Date object for your birthday. Write functions to extract the year,
month, day, and calculate your age dynamically.
6. Object Freezing:
Create an object and use Object.freeze to make it immutable. Write a
program that tries to modify, delete, or add properties to the frozen object.
Log the results.
JavaScript Task’s 5
Write a function that converts an array of key-value pairs into an object.
Example: [["name", "Alice"], ["age", 25]] should become {name: "Alice", age:
25} .
Create a deeply nested object that contains arrays, functions, and other
objects. Write a recursive function to print all primitive values inside it.
Function’s
Tasks On Functions
1. Function Returning Data Type:
Write a function getType that accepts a variable and returns its type. If it's a
reference type like an array or object, return "array" or "object" instead of
"object."
Create a function calculate that takes three arguments: two numbers and an
operator ( + , - , * , / ). Perform the operation and return the result.
4. String Manipulation:
Create a function capitalize that takes a string and returns it with the first
letter capitalized and the rest in lowercase.
JavaScript Task’s 6
Write a function isTruthy that accepts any value and returns true if the
value is truthy, and false otherwise. Test it with various data types and
edge cases like 0 , NaN , null , and empty strings.
Write a function findMax that takes three numbers as arguments and returns
the largest one using conditional operators.
9. Calculator Object:
Create an object calculator with methods to add, subtract, multiply, and
divide two numbers. Call these methods and display the results.
Create a function that declares a variable with the same name as a global
variable. Demonstrate how to access both the local and global variables
using the this keyword and scope rules.
JavaScript Task’s 7
Write a function convertTemperature that converts temperatures between
Celsius, Fahrenheit, and Kelvin. Use conditionals to determine the
conversion logic.
Write a function createObject that takes a key and a value as arguments and
returns an object with that key and value dynamically assigned.
Write a function isValidOperator that takes a string as input and checks if it's
a valid operator ( + , - , * , / ). Use it to validate inputs in a calculator
function.
Conditional Statement’s
Tasks on Conditional Statements (using if , else , else if , switch , and ternary
operators):
Conditional Statements
JavaScript Task’s 8
1. Find Even or Odd:
Write a function checkEvenOdd that takes a number as an argument and
checks if it is even or odd. Use an if...else statement.
2. Grade Classification:
Write a function classifyGrade that takes a score as an argument and returns
the grade ( A , B , C , D , or F ) based on the following scale:
4. Temperature Advice:
Write a function getTemperatureAdvice that accepts the temperature in Celsius
and provides advice based on the value.
6. Ternary Operator:
Write a function checkAdult that takes a person's age and uses the ternary
operator to return "Adult" if the age is 18 or older, otherwise "Minor" .
JavaScript Task’s 9
Write a function isDivisible that checks if a number is divisible by 3, 5, or
both. Use if...else and check each condition.
8. Discount Calculation:
9. Weekend Check:
Write a function isWeekend that takes a day (as a string) and returns true if
it's Saturday or Sunday, otherwise false .
Write a function that accepts a number and checks if it’s between 10 and
100. Use a combination of conditional statements and data types to handle
edge cases like non-numeric inputs.
JavaScript Task’s 10
If the length is more than 10, return "Long"
Write a function classifyAgeGroup that takes an age and classifies it into one
of the following groups:
"Teenager" (13–19)
"Adult" (20–64)
"Senior" (65+)
"Valid grade" if it’s one of the valid grades, otherwise "Invalid grade".
JavaScript Task’s 11
Write a function checkDivisibility that checks if a number is divisible by 3 or
5, but not both. Use a combination of conditional statements and operators.
2. Array Length:
Write a function that returns the length of an array. Test it with different
types of arrays (empty, non-empty).
modified array.
Write a function that accepts an array and an index, then returns the
element at that index.
6. Array Slice:
Write a function that returns a sliced array from index 2 to index 5. Use the
slice() method.
7. Array Splice:
Write a function that removes 2 elements from index 3 in an array using
splice() , and adds two new elements in their place.
8. Concatenate Arrays:
JavaScript Task’s 12
Write a function that concatenates two arrays using the concat() method
and returns the resulting array.
method.
Write a function that checks if a specific number exists in an array using the
includes() method.
JavaScript Task’s 13
Write a function that sorts an array of numbers in ascending order using the
sort() method.
Write a function that finds the first element in an array greater than 50 using
the find() method.
method.
Write a function that sorts an array of strings by their length using the
sort() method.
JavaScript Task’s 14
array of person objects).
JavaScript Task’s 15
7. Extract Numbers Greater Than a Given Value:
Write a function that takes an array of numbers and a threshold value.
Return a new array that includes only the numbers greater than the
threshold, using filter() .
8. Array of Booleans:
Write a function that takes an array of mixed data types (numbers, strings,
booleans) and returns an array containing only boolean values.
Write a function that takes a string and returns the reversed version of the
string.
2. Convert to Uppercase:
Write a function that takes a string and returns the string converted to
uppercase.
3. Convert to Lowercase:
Write a function that takes a string and returns the string converted to
lowercase.
4. String Length:
Write a function that takes a string and returns its length.
5. Concatenate Strings:
JavaScript Task’s 16
Write a function that concatenates two strings and returns the combined
result.
7. String Index:
Write a function that returns the index of the first occurrence of a character
in a string using the indexOf() method.
8. Extract Substring:
Write a function that extracts a substring from a string starting from index 2
to index 5 using the substring() method.
9. String Search:
Write a function that searches for a word in a string and returns the position
of the first match using the search() method.
method.
Write a function that joins an array of words into a single string with a space
separator using the join() method.
JavaScript Task’s 17
Write a function that checks if a string ends with a specific substring using
the endsWith() method.
Write a function that checks if a string is empty or not and returns true or
false .
JavaScript Task’s 18
26. Replace First Occurrence of Substring:
Write a function that replaces the first occurrence of a word "hello" with
"hi" in a string using the replace() method.
JavaScript Task’s 19
5. Find and Replace Multiple Substrings:
Write a function that finds and replaces multiple substrings in a string using
the replace() method with a regular expression.
Write a function that counts how often each character appears in a string
and returns the result as an object.
JavaScript Task’s 20
Write a function that removes all whitespace characters from a string using
a regular expression.
JavaScript Task’s 21
Write a function that rounds a number down to the nearest integer using
Math.floor() .
JavaScript Task’s 22
19. Get Exponent of a Number:
Write a function that returns the exponent of a number using
Math.exponent() .
Write a function that formats a large number with commas (e.g., 1000000 →
1,000,000 ).
JavaScript Task’s 23
30. Convert a Floating-Point Number to an Integer:
Write a function that converts a floating-point number to an integer by
rounding it using Math.round() .
7. Find the Sum of Array Elements that are Greater than a Threshold:
Write a function that returns the sum of all array elements greater than a
given threshold using filter() and reduce() .
Write a function that counts how many negative and positive numbers are in
an array.
JavaScript Task’s 24
Write a function that finds the average of all numbers in an array of strings
representing numbers.
Loop’s
Loops
1. Print Numbers from 1 to 10 Using for Loop:
Write a function that prints numbers from 1 to 10 using a for loop.
loop.
JavaScript Task’s 25
Write a function that finds the sum of numbers from 1 to 50 using a while
loop.
loop.
JavaScript Task’s 26
Write a function that prints numbers from 1 to N (given by the user) using a
while loop.
20. Print All Prime Numbers from 1 to 100 Using for Loop:
Write a function that prints all prime numbers between 1 and 100 using a
for loop.
loop.
27. Find the Average of All Odd Numbers in an Array Using for Loop:
Write a function that calculates the average of all odd numbers in an array
using a for loop.
28. Check if All Array Elements are Positive Using for Loop:
Write a function that checks if all elements in an array are positive numbers
using a for loop.
JavaScript Task’s 27
Combined with Previous Topics (Variables, Operators, Data
Types, Functions, Conditional Statements, Arrays, Strings,
Numbers)
1. Find Sum of Array of Strings Representing Numbers:
Write a function that calculates the sum of an array of strings that represent
numbers (e.g., ["1", "2", "3"] ).
loop.
10. Find the Greatest Common Divisor (GCD) of Two Numbers Using a Loop:
JavaScript Task’s 28
Write a function that calculates the greatest common divisor (GCD) of two
numbers using a loop.
Type Conversion’s
Type Conversion (Current Topic)
1. Convert a String to a Number:
Write a function that takes a string as input (e.g., "123" ) and returns its
numerical value.
respectively.
JavaScript Task’s 29
What will the following code output?
Write a function that takes an array [1, 2, 3] and converts it into a string
"1,2,3" .
object.
JavaScript Task’s 30
Write a function that converts a boolean value ( true or false ) to a string
using String() .
Write a function that takes a string with leading and trailing spaces and
returns it after trimming the spaces.
JavaScript Task’s 31
What will the following code output?
JavaScript Task’s 32
Write a function that converts an array of mixed types [1, "hello", true,
💡 Note: Mention the reason for the answer your are giving (Mandatory).
1. Question 1:
console.log(3 + 2 + '5');
2. Question 2:
let a = 1;
let b = '1';
JavaScript Task’s 33
console.log(a == b);
console.log(a === b);
3. Question 3:
console.log('5' - 3);
4. Question 4:
console.log('10' * '2');
5. Question 5:
let x = null;
let y = undefined;
console.log(x == y);
console.log(x === y);
6. Question 6:
console.log(0 == false);
console.log(0 === false);
7. Question 7:
console.log(1 + true);
console.log(1 + false);
8. Question 8:
JavaScript Task’s 34
let arr = [1, 2, 3];
console.log(arr + '4');
9. Question 9:
console.log('1' + 2 - 1);
console.log([] == ![]);
let obj = { a: 1, b: 2 };
let obj2 = { a: 1, b: 2 };
console.log(obj == obj2);
let a = [];
let b = [];
console.log(a == b);
console.log(a === b);
console.log(!!'0');
console.log(!!0);
JavaScript Task’s 35
14. Question 14:
console.log([] == false);
console.log([] == true);
console.log('5' == 5);
console.log('5' === 5);
console.log(0 || 1);
console.log(1 || 0);
console.log(0 && 1);
console.log(1 && 0);
let x = 5;
let y = 10;
console.log(x > y ? 'Greater' : 'Smaller');
JavaScript Task’s 36
console.log(5 + 3 + '2');
console.log('5' + 3 + 2);
let obj = { a: 1, b: 2 };
console.log(obj.a);
delete obj.a;
console.log(obj.a);
let x = '10';
let y = '20';
console.log(x + y);
console.log(Number(x) + Number(y));
JavaScript Task’s 37
console.log(Boolean('false'));
console.log(Boolean(''));
console.log('abc'.length);
console.log('abc'.slice(0, 2));
let num = 3;
console.log(num++);
console.log(++num);
let num = 3;
console.log(num--);
console.log(--num);
console.log(typeof NaN);
console.log(typeof null);
JavaScript Task’s 38
console.log(typeof undefined);
let a = 'Hello';
let b = 'World';
console.log(a + ' ' + b);
JavaScript Task’s 39
let x = 'Hello';
let y = 'World';
console.log(x.concat(' ', y));
JavaScript Task’s 40