0% found this document useful (0 votes)
22 views

JavaScript(40%) Tasks 9th Jan 2025

The document outlines a series of JavaScript tasks categorized into variables, operators, data types, functions, and conditional statements. Each category contains specific tasks aimed at enhancing understanding of JavaScript concepts, such as variable scope, type conversion, and function behavior. The tasks are designed to be practical exercises for learners to apply their knowledge and improve their coding skills.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

JavaScript(40%) Tasks 9th Jan 2025

The document outlines a series of JavaScript tasks categorized into variables, operators, data types, functions, and conditional statements. Each category contains specific tasks aimed at enhancing understanding of JavaScript concepts, such as variable scope, type conversion, and function behavior. The tasks are designed to be practical exercises for learners to apply their knowledge and improve their coding skills.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

JavaScript Task’s

Variable’s & Operator’s


Tasks for Variables
1. Scope Challenge:
Declare three variables: one in the global scope, one in a function scope,
and one in a block scope. Write a program that demonstrates the difference
in their accessibility.

2. Dynamic Typing Puzzle:


Create a variable with a number type and convert it to string , then to
boolean , and back to number . Write a function to check the type at each

stage and log it.

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.

Tasks for Operators


1. Custom Exponentiation:
Create a function that takes two numbers and calculates the result of the
first number raised to the power of the second number without using the
** operator.

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

<= c) || !(c > d) . Provide test cases with edge values.

4. FizzBuzz with Operators:


Write a FizzBuzz implementation using only the ternary ( ? : ) operator.

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).

Tasks Combining Both


1. String Concatenation with Variables:
Take two variables containing numbers. Concatenate them as strings (e.g.,
5 and 6 should become 56 ) and then convert them back to numbers (e.g.,

56 to 56 ).

2. Logical XOR Implementation:

Write a function to simulate the behavior of a logical XOR using only logical
operators ( && , || , ! ).

3. Default Value Simulation:

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 .

4. String Template Challenge:


Write a program that combines variables in a single template literal and
outputs a meaningful sentence. Example: "The result of ${a} + ${b} is ${a +
b}."

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.

6. Compound Assignment Operators:

Create a function that takes a number and uses all compound assignment
operators ( += , -= , *= , /= , %= ) to produce a sequence of results.

7. Falsy and Truthy Fun:


Test all JavaScript falsy values ( false , 0 , null , undefined , NaN , '' ) with
logical operators and log how they behave.

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.

10. Complex Variable Math:

Write a program that declares three variables a , b , and c . Calculate the


value of (a + b) * c - b/c , ensuring proper operator precedence without
parentheses.

Data Type’s

Tasks for Primitive Data Types


1. Identify the Type:

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.

5. Null and Undefined:

Write a program to explain the difference between null and undefined . Use
examples to show how they behave in equality ( == ) and strict equality
( === ) comparisons.

Tasks for Reference Data Types


1. Object Deep Copy:
Create an object with nested properties and write a function to deep copy
it. Compare it with shallow copy techniques.

2. Dynamic Object Properties:

Write a program that dynamically adds, deletes, and modifies properties of


an object using both dot notation and bracket notation.

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:

Create a program that demonstrates the difference between a Map and a


regular object. Use examples to show how keys of different types behave in

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 .

Tasks Combining Both


1. Type Coercion Puzzle:

Write a program that shows type coercion between different types in


operations like addition ( + ), subtraction ( - ), and comparisons ( == , === ).
Explain the results.

2. Falsy and Truthy:

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.

3. Data Type Constructor:

Write a program that demonstrates the use of constructors like String ,


Number , Boolean , and Object to convert data types.

4. Date Object Challenge:

Create a Date object for your birthday. Write functions to extract the year,
month, day, and calculate your age dynamically.

5. Custom Type Checker:

Implement a function customTypeOf that mimics the behavior of the typeof


operator but provides more detailed output (e.g., "array" instead of "object"
for arrays).

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.

7. Type Conversion Chain:

Convert a number to a string, then to an array of characters, then back to a


string, and finally to a number. Write a function that does this in one go.

8. Array to Object Conversion:

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} .

9. Function as Data Type:


Write a function that takes another function as an argument and logs its
data type. Call the function to demonstrate that functions are also data
types.

10. Nested Data Types:

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."

2. Arithmetic Operations in Functions:

Create a function calculate that takes three arguments: two numbers and an
operator ( + , - , * , / ). Perform the operation and return the result.

3. Switching Between Data Types:


Write a function convertDataType that takes two parameters: a value and a
target type ( "string" , "number" , "boolean" ). Convert the value to the specified
type and return it.

4. String Manipulation:

Create a function capitalize that takes a string and returns it with the first
letter capitalized and the rest in lowercase.

5. Truthy and Falsy Checker:

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.

6. Max of Three Numbers:

Write a function findMax that takes three numbers as arguments and returns
the largest one using conditional operators.

7. Nested Objects and Functions:


Create an object that contains nested objects and arrays. Write a function
that navigates through the structure and returns all primitive values.

8. Array and Function:

Write a function doubleNumbers that takes an array of numbers and returns a


new array with all values doubled. Use the map method.

9. Calculator Object:
Create an object calculator with methods to add, subtract, multiply, and
divide two numbers. Call these methods and display the results.

10. Dynamic Variable Scope:

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.

11. Function as a Data Type:


Write a function operation that accepts another function as an argument and
applies it to two numbers. For example:

operation((a, b) => a * b, 4, 5); // Output: 20

12. Recursive Type Checker:


Write a recursive function deepTypeChecker that takes an object or array and
logs the data type of every value within it.

13. Palindrome Checker:


Create a function isPalindrome that checks if a given string or number is a
palindrome. Return true or false .

14. Temperature Converter:

JavaScript Task’s 7
Write a function convertTemperature that converts temperatures between
Celsius, Fahrenheit, and Kelvin. Use conditionals to determine the
conversion logic.

15. Dynamic Object Keys with Functions:

Write a function createObject that takes a key and a value as arguments and
returns an object with that key and value dynamically assigned.

16. Count Data Types in an Array:


Write a function countTypes that takes an array and returns an object with
the count of each data type (e.g., {string: 3, number: 2, boolean: 1} ).

17. Validate Operator Input:

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.

18. Function Hoisting Test:


Write a program that demonstrates function hoisting by calling a function
before its declaration. Include both function declarations and function
expressions to compare behavior.

19. Default and Rest Parameters:


Create a function sumAll that takes a required first number and any number
of additional numbers (using rest parameters). Return the sum of all
arguments.

20. Higher-Order Function with Data Types:


Write a higher-order function filterByType that takes an array and a type
(e.g., "number" ) and returns an array of elements matching that type.

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:

A for scores >= 90

B for scores >= 80

C for scores >= 70

D for scores >= 60

F for scores < 60

3. Leap Year Checker:


Write a function isLeapYear that checks if a given year is a leap year. A year
is a leap year if it is divisible by 4, except for years divisible by 100 unless
they are divisible by 400.

4. Temperature Advice:
Write a function getTemperatureAdvice that accepts the temperature in Celsius
and provides advice based on the value.

Below 0°C: "It's freezing!"

0°C - 10°C: "It's cold."

11°C - 20°C: "It's cool."

21°C - 30°C: "It's warm."

Above 30°C: "It's hot!"

5. Switch Case Day of the Week:


Write a function getDayName that takes a number (0–6) as input and returns
the name of the corresponding day of the week using a switch statement.

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" .

7. Evenly Divisible Check:

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:

Write a function applyDiscount that takes a price and a boolean value


indicating if the customer has a discount. If the customer has a discount,
apply a 20% discount, otherwise return the original price.

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 .

10. Age and Voting Eligibility:

Write a function canVote that checks if a person is eligible to vote (18 or


older) based on their age.

Tasks Combining Conditional Statements with Previous Topics


(Variables, Operators, Data Types, Functions)
1. Number Range Check:

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.

2. Price Range Calculator:

Create a function calculateDiscount that takes a price and applies the


following discount rules:

For prices above 1000, apply a 20% discount.

For prices between 500 and 1000, apply a 10% discount.

Prices below 500 receive no discount.


Return the final price after discount.

3. Categorize String Length:

Write a function categorizeString that takes a string as input and categorizes


it based on its length:

If the length is less than 5, return "Short"

If the length is between 5 and 10, return "Medium"

JavaScript Task’s 10
If the length is more than 10, return "Long"

Test it with different string lengths.

4. Odd or Even Array:

Write a function evenOrOddArray that takes an array of numbers and returns a


new array containing strings "Even" or "Odd" based on whether the number
is even or odd.

5. Find Largest of Three Numbers:


Write a function findLargest that takes three numbers as arguments and
returns the largest number using if...else or switch .

6. Age Group Classifier:

Write a function classifyAgeGroup that takes an age and classifies it into one
of the following groups:

"Child" (age <= 12)

"Teenager" (13–19)

"Adult" (20–64)

"Senior" (65+)

7. Temperature to Fahrenheit or Celsius Converter:


Write a function convertTemperature that takes a temperature value and a flag
( "C" or "F" ) indicating the conversion direction. If "C" , convert the
temperature from Fahrenheit to Celsius, and vice versa.

8. Grade Validator with Ternary:


Write a function validateGrade that uses the ternary operator to check if a
given grade is valid ( "A" , "B" , "C" , "D" , "F" ) and returns a message:

"Valid grade" if it’s one of the valid grades, otherwise "Invalid grade".

9. Discount Eligibility with Logical Operators:


Write a function isEligibleForDiscount that takes a user’s membership status
( true or false ) and a minimum purchase amount and returns "Eligible" for
a discount if the user is a member and their purchase is above a threshold
amount.

10. Divisible by 3 or 5 Check:

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.

Array’s & Array’s Method’s


Arrays and Array Methods
1. Create an Array of Numbers:

Write a program to create an array of numbers from 1 to 10.

2. Array Length:
Write a function that returns the length of an array. Test it with different
types of arrays (empty, non-empty).

3. Push and Pop:


Write a function that adds a number to the end of an array using push() and
then removes the last element using pop() . Return the modified array.

4. Shift and Unshift:


Write a function that adds a number to the beginning of an array using
unshift() and then removes the first element using shift() . Return the

modified array.

5. Accessing Array Elements:

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.

9. Array Iteration with For Loop:


Write a function that iterates over an array and prints each element using a
for loop.

10. Array Iteration with forEach:


Write a function that iterates over an array and prints each element using
the forEach() method.

11. Array Map:


Write a function that doubles each number in an array using the map()

method and returns a new array.

12. Filter Even Numbers:


Write a function that takes an array of numbers and filters out only the even
numbers using the filter() method.

13. Find Maximum Value:


Write a function that finds the maximum value in an array using the reduce()

method.

14. Sum of Array Elements:


Write a function that calculates the sum of all elements in an array using the
reduce() method.

15. Array Includes:

Write a function that checks if a specific number exists in an array using the
includes() method.

16. Find Element Index:


Write a function that finds the index of a specific element in an array using
the indexOf() method.

17. Array Join:


Write a function that joins all elements of an array into a string with a
comma separator using the join() method.

18. Sort an Array:

JavaScript Task’s 13
Write a function that sorts an array of numbers in ascending order using the
sort() method.

19. Reverse an Array:


Write a function that reverses an array using the reverse() method.

20. Array of Unique Values:


Write a function that takes an array with duplicate values and returns a new
array with only unique values using the Set() constructor.

21. Array Find Method:

Write a function that finds the first element in an array greater than 50 using
the find() method.

22. Array Some Method:


Write a function that checks if some elements in an array are greater than
50 using the some() method.

23. Array Every Method:


Write a function that checks if all elements in an array are positive numbers
using the every() method.

24. Array Flat Method:


Write a function that flattens a 2D array into a 1D array using the flat()

method.

25. Array Sort by Length:

Write a function that sorts an array of strings by their length using the
sort() method.

26. Find First Non-Null Element:


Write a function that returns the first non-null element in an array using the
find() method.

27. Combine Array of Strings:


Write a function that combines an array of strings into a single string where
each word is separated by a space, using join() .

28. Map Array of Objects:


Write a function that maps through an array of objects and returns an array
of a specific property from each object (e.g., extracting names from an

JavaScript Task’s 14
array of person objects).

29. Filter and Transform:


Write a function that filters out negative numbers from an array and
multiplies the remaining numbers by 2 using filter() and map() .

30. Flatten Deeply Nested Arrays:


Write a function that flattens an array with arbitrary nesting levels (e.g., 3D
arrays) into a single array using recursion and flat() .

Combined with Previous Topics (Variables, Operators, Data


Types, Functions)
1. Sum of Even and Odd Numbers:
Write a function sumEvenOdd that takes an array of numbers, separates the
even and odd numbers, sums them separately, and returns both sums. Use
conditional statements and reduce() .

2. Array with Mixed Types:


Write a function that accepts an array containing numbers, strings, and
booleans, and returns an array with only numeric values.

3. Array Manipulation with Functions:

Write a function processArray that takes an array and a callback function.


The callback should either filter or transform the elements of the array (e.g.,
filtering out negative numbers or doubling all elements).

4. Find Duplicates in an Array:


Write a function that checks if there are any duplicate values in an array
and returns true or false using indexOf() and filter() .

5. Transform Object Array:


Given an array of objects, write a function transformObjects that uses map()
to extract specific properties from each object and return an array of those
values.

6. Merge Two Arrays of Different Data Types:


Write a function that merges two arrays (one of numbers and the other of
strings) into a single array. Then, find and return the first string that starts
with a vowel using find() .

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.

9. Remove Falsy Values from Array:


Write a function that removes all falsy values ( false , null , 0 , NaN ,
undefined , "" ) from an array using filter() .

10. Map and Filter Together:


Write a function that takes an array of numbers, filters out numbers smaller
than 10, and doubles the remaining numbers using filter() and map() .

String’s & String Method’s


Strings and String Methods
1. Reverse a String:

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.

6. Check if String Contains Substring:


Write a function that checks if a string contains a specific substring using
the includes() method.

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.

10. Trim Whitespace:


Write a function that removes any leading or trailing whitespace from a
string using the trim() method.

11. Replace Substring:


Write a function that replaces all occurrences of the word "cat" with "dog"
in a string using the replace() method.

12. Split String into Array:


Write a function that splits a string into an array of words using the split()

method.

13. Join Array into String:

Write a function that joins an array of words into a single string with a space
separator using the join() method.

14. Check if String Starts with Substring:


Write a function that checks if a string starts with a specific substring using
the startsWith() method.

15. Check if String Ends with Substring:

JavaScript Task’s 17
Write a function that checks if a string ends with a specific substring using
the endsWith() method.

16. Repeat String:


Write a function that repeats a string a given number of times using the
repeat() method.

17. Extract Last N Characters:


Write a function that extracts the last N characters from a string using the
slice() method.

18. Convert First Character to Uppercase:


Write a function that takes a string and returns the string with the first
character converted to uppercase and the rest to lowercase.

19. Check if String is Empty:

Write a function that checks if a string is empty or not and returns true or
false .

20. Find the Length of Each Word in a String:


Write a function that takes a sentence (string) and returns an array with the
length of each word in the sentence.

21. Count Occurrences of a Substring:


Write a function that counts how many times a substring appears in a
string.

22. String to Array of Characters:


Write a function that converts a string into an array of characters.

23. Remove Vowels from String:


Write a function that removes all vowels from a string using regular
expressions and returns the modified string.

24. String to Title Case:


Write a function that converts a string to title case (e.g., "hello world" →
"Hello World").

25. Find the Position of the Last Occurrence of a Character:


Write a function that finds the position of the last occurrence of a character
in a string using the lastIndexOf() method.

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.

27. Check for Substring with Case Sensitivity:

Write a function that checks if a string contains a specific substring in a


case-insensitive manner.

28. Escape Special Characters:


Write a function that escapes special characters in a string (e.g., & , < , > ,
" ).

29. Pad String:


Write a function that pads a string with a specific character until it reaches
a certain length using the padStart() or padEnd() method.

30. Remove All Non-Alphanumeric Characters:


Write a function that removes all non-alphanumeric characters from a string
using a regular expression.

Combined with Previous Topics (Variables, Operators, Data


Types, Functions, Conditional Statements)
1. Check if String is Palindrome:
Write a function that checks if a string is a palindrome (reads the same
forwards and backwards). Use a combination of string methods and
conditional statements.

2. Count Words in a String:

Write a function that counts the number of words in a string. Consider


words separated by spaces and use split() to count them.

3. Replace All Numbers with Word:


Write a function that takes a string and replaces all numeric digits with the
word "number." Use replace() with a regular expression.

4. Remove Duplicate Characters:


Write a function that removes all duplicate characters from a string, leaving
only the first occurrence of each character.

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.

6. Check if String is a Number:

Write a function that checks if a string represents a valid number (e.g.,


"123" or "-42") using parseInt() and isNaN() .

7. Swap Case in String:


Write a function that swaps the case of each letter in a string (e.g., "Hello"
→ "hELLO") using split() , map() , and join() .

8. Convert String into Object (JSON Parsing):


Write a function that converts a string representing a JSON object into a
JavaScript object using JSON.parse() .

9. Check if String Contains Only Letters and Numbers:


Write a function that checks if a string contains only letters and numbers
using a regular expression.

10. Character Frequency Counter:

Write a function that counts how often each character appears in a string
and returns the result as an object.

11. Generate Random String:


Write a function that generates a random string of a given length consisting
of alphanumeric characters.

12. Capitalize Each Word in a String:


Write a function that capitalizes the first letter of each word in a string using
split() , map() , and join() .

13. Validate Email Address Format:


Write a function that checks if a given string is a valid email address format
using regular expressions.

14. Count Vowels in a String:


Write a function that counts the number of vowels in a string using a regular
expression.

15. Remove All Whitespaces:

JavaScript Task’s 20
Write a function that removes all whitespace characters from a string using
a regular expression.

Number’s & Number Method’s


Numbers and Number Methods
1. Convert String to Number:
Write a function that converts a string representing a number (e.g., "42" ) to
an actual number using Number() .

2. Check if Number is Integer:


Write a function that checks if a given number is an integer using
Number.isInteger() .

3. Find Maximum Number in an Array:


Write a function that finds the maximum number in an array of numbers
using Math.max() .

4. Find Minimum Number in an Array:


Write a function that finds the minimum number in an array of numbers
using Math.min() .

5. Generate Random Number:


Write a function that generates a random number between 1 and 100 using
Math.random() .

6. Round Number to Nearest Integer:


Write a function that rounds a number to the nearest integer using
Math.round() .

7. Round Number Up:


Write a function that rounds a number up to the nearest integer using
Math.ceil() .

8. Round Number Down:

JavaScript Task’s 21
Write a function that rounds a number down to the nearest integer using
Math.floor() .

9. Get Absolute Value of a Number:

Write a function that returns the absolute value of a number using


Math.abs() .

10. Power of a Number:


Write a function that calculates the power of a number (e.g., 343^4) using
Math.pow() .

11. Square Root of a Number:


Write a function that calculates the square root of a number using
Math.sqrt() .

12. Get the Square of a Number:

Write a function that returns the square of a given number.

13. Convert Number to String:


Write a function that converts a number to a string using String() or
toString() .

14. Check if Number is NaN:


Write a function that checks if a given value is NaN (Not-a-Number) using
Number.isNaN() .

15. Convert Floating Point Number to Integer:

Write a function that converts a floating point number to an integer using


Math.trunc() .

16. Format Number to Fixed Decimal Places:


Write a function that formats a number to two decimal places using
toFixed() .

17. Check if Number is Finite:


Write a function that checks if a given number is finite (i.e., not Infinity or
NaN) using Number.isFinite() .

18. Get the Sign of a Number:


Write a function that returns the sign of a number (-1, 0, or 1) using
Math.sign() .

JavaScript Task’s 22
19. Get Exponent of a Number:
Write a function that returns the exponent of a number using
Math.exponent() .

20. Clamp a Number Between Two Values:


Write a function that clamps a number between a minimum and maximum
value (inclusive) using Math.max() and Math.min() .

21. Convert a Number to Binary:


Write a function that converts a decimal number to its binary equivalent
using toString(2) .

22. Convert a Number to Hexadecimal:


Write a function that converts a decimal number to its hexadecimal
equivalent using toString(16) .

23. Check if a Number is Even or Odd:


Write a function that checks if a number is even or odd using modulo ( % ).

24. Sum of Digits in a Number:


Write a function that returns the sum of the digits of a given number (e.g.,
123 → 1 + 2 + 3 = 6 ).

25. Generate a Random Integer Within a Range:


Write a function that generates a random integer between two values
(inclusive).

26. Count the Number of Digits in a Number:


Write a function that counts how many digits are in a number.

27. Format Number with Commas:

Write a function that formats a large number with commas (e.g., 1000000 →

1,000,000 ).

28. Check if a Number is a Prime Number:


Write a function that checks if a number is prime (i.e., divisible only by 1 and
itself).

29. Calculate Percentage:


Write a function that calculates the percentage of a number (e.g., 25% of
200).

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() .

Combined with Previous Topics (Variables, Operators, Data


Types, Functions, Conditional Statements, Arrays, Strings)
1. Find the Sum of an Array of Numbers:
Write a function that calculates the sum of all the numbers in an array of
numbers using reduce() .

2. Sum of Digits in a String:


Write a function that extracts all the digits from a string and returns their
sum.

3. Find the Maximum Number in an Array of Strings (Numerically):


Write a function that finds the maximum number in an array of string
representations of numbers (e.g., ["1", "10", "100"] ).

4. Find the Largest Palindrome Number:


Write a function that finds the largest palindrome number in an array of
numbers.

5. Calculate the Area of a Circle from Radius Input:


Write a function that calculates the area of a circle (πr²) using the radius
input as a number.

6. Generate a Random Floating-Point Number Between Two Values:


Write a function that generates a random floating-point number between
two values (inclusive) using Math.random() .

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() .

8. Count Negative and Positive Numbers in an Array:

Write a function that counts how many negative and positive numbers are in
an array.

9. Find the Average of All Numbers in an Array of Strings:

JavaScript Task’s 24
Write a function that finds the average of all numbers in an array of strings
representing numbers.

10. Create a Function that Returns Fibonacci Series Up to a Given Limit:


Write a function that returns the Fibonacci series up to a given number
using recursion or iteration.

11. Validate if a String Can Be Converted to a Number:


Write a function that validates if a string can be converted to a valid number
and return the number, or a message indicating it can't be converted.

12. Convert an Array of Numbers to a Single Number (Concatenation):


Write a function that converts an array of numbers into a single number by
concatenating all elements together.

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.

2. Print Even Numbers from 1 to 20 Using for Loop:


Write a function that prints even numbers from 1 to 20 using a for loop.

3. Print Odd Numbers from 1 to 20 Using while Loop:


Write a function that prints odd numbers from 1 to 20 using a while loop.

4. Print Multiplication Table of a Number Using for Loop:

Write a function that prints the multiplication table of a number (e.g., 2)


from 1 to 10 using a for loop.

5. Print Numbers from 1 to 100 Divisible by 5 Using for Loop:


Write a function that prints numbers from 1 to 100 divisible by 5 using a for

loop.

6. Sum of Numbers from 1 to 50 Using while Loop:

JavaScript Task’s 25
Write a function that finds the sum of numbers from 1 to 50 using a while

loop.

7. Find Factorial of a Number Using for Loop:


Write a function that calculates the factorial of a number using a for loop.

8. Reverse a String Using a for Loop:

Write a function that reverses a given string using a for loop.

9. Print Array Elements Using for Loop:


Write a function that prints all elements of an array using a for loop.

10. Find Sum of Array Elements Using for Loop:


Write a function that calculates the sum of all elements in an array using a
for loop.

11. Count Occurrences of a Specific Element in an Array Using for Loop:


Write a function that counts how many times a specific number appears in
an array.

12. Check if a Number is Prime Using for Loop:


Write a function that checks if a given number is prime using a for loop.

13. Print Fibonacci Series Using while Loop:


Write a function that prints the Fibonacci series up to a given number using
a while loop.

14. Find the Largest Number in an Array Using for Loop:


Write a function that finds the largest number in an array using a for loop.

15. Find the Smallest Number in an Array Using while Loop:


Write a function that finds the smallest number in an array using a while

loop.

16. Print Pattern of Stars (Pyramid) Using for Loop:


Write a function that prints a pyramid pattern of stars using a for loop (e.g.,
* , ** , *** ).

17. Sum of All Even Numbers in an Array Using for Loop:


Write a function that sums all even numbers in an array using a for loop.

18. Print Numbers from 1 to N (User Input) Using while Loop:

JavaScript Task’s 26
Write a function that prints numbers from 1 to N (given by the user) using a
while loop.

19. Print a Countdown Using do-while Loop:


Write a function that prints a countdown from 10 to 1 using a do-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.

21. Multiply Each Element of an Array by 2 Using for Loop:


Write a function that multiplies each number in an array by 2 using a for

loop.

22. Find the Average of Numbers in an Array Using for Loop:


Write a function that calculates the average of numbers in an array using a
for loop.

23. Filter Odd Numbers from an Array Using for Loop:


Write a function that returns an array containing only the odd numbers from
a given array using a for loop.

24. Print a Table of Powers of 2 Using for Loop:


Write a function that prints a table of powers of 2 (e.g., 2^1, 2^2, 2^3, etc.)
up to a given number using a for loop.

25. Reverse an Array Using while Loop:


Write a function that reverses an array of numbers using a while loop.

26. Create a Reverse Pyramid of Numbers Using for Loop:


Write a function that creates a reverse pyramid of numbers, e.g., 5 4 3 2 1 ,
using a for 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"] ).

2. Create a String of Numbers from 1 to N Using a Loop:


Write a function that generates a string of numbers from 1 to N (e.g., "1 2 3
4 5") using a loop.

3. Count Occurrences of a Word in a Sentence Using a Loop:


Write a function that counts how many times a word appears in a sentence
using a loop.

4. Find Largest Even Number in an Array Using for Loop:


Write a function that finds the largest even number in an array of numbers
using a for loop.

5. Create an Array of Even Numbers from 1 to 100 Using a Loop:


Write a function that creates an array of even numbers from 1 to 100 using a
loop.

6. Sum All Positive Numbers in an Array Using a Loop:


Write a function that sums all positive numbers in an array of mixed
numbers (positive and negative).

7. Convert Array of Numbers to Strings Using a Loop:


Write a function that converts an array of numbers into an array of strings
using a loop.

8. Find Numbers Divisible by 3 in an Array Using for Loop:


Write a function that finds all numbers divisible by 3 in an array using a for

loop.

9. Reverse a String and Compare it to the Original to Check for Palindrome:


Write a function that reverses a string using a loop and compares it to the
original string to check if it's a palindrome.

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.

2. Convert a Number to a String:


Write a function that converts a number (e.g., 456 ) into a string.

3. Implicit Type Conversion:


What will the following code output?

let result = '5' + 10;


console.log(result);

4. Explicit Type Conversion Using Number() :


Convert a string "123.45" to a number using Number() .

5. Convert a String to a Boolean:

Write a function that converts a non-empty string to true and an empty


string to false .

6. Convert a Boolean to a String:


Write a function that converts a boolean value ( true or false ) to its string
equivalent.

7. Convert a Boolean to a Number:


Write a function that converts a boolean value ( true or false ) to 1 or 0

respectively.

8. Convert null to a Number:

JavaScript Task’s 29
What will the following code output?

let value = null;


console.log(Number(value));

9. Convert undefined to a Number:

What will the following code output?

let value = undefined;


console.log(Number(value));

10. Convert Array to String:

Write a function that takes an array [1, 2, 3] and converts it into a string
"1,2,3" .

11. Convert an Object to a Primitive Value:


Write a function that converts an object to a primitive value (like a number
or string).

12. Convert an Empty String to a Boolean:


Write a function that converts an empty string ( "" ) to a boolean ( false ).

13. Convert NaN to a Boolean:

What will the following code output?

let value = NaN;


console.log(Boolean(value));

14. Convert String to a Number Using parseInt() and parseFloat() :


Write a function that converts the string "12.34" into an integer using
parseInt() and the same string into a float using parseFloat() .

15. String to Date Conversion:


Write a function that converts a string "2023-01-01" into a JavaScript Date

object.

16. Boolean to String Conversion with String() :

JavaScript Task’s 30
Write a function that converts a boolean value ( true or false ) to a string
using String() .

17. Boolean Type Conversion in Conditions:


What will the following code output?

let value = "some text";


if (Boolean(value)) {
console.log("Converted to true");
} else {
console.log("Converted to false");
}

18. Convert a Date Object to a String:


Write a function that converts a Date object into a readable string format.

19. Convert a String to a Number and Add It to Another Number:


Write a function that converts the string "50" to a number and adds it to
another number ( 25 ).

20. Convert an Array of Numbers to Strings Using map() :


Write a function that converts an array of numbers [1, 2, 3] to an array of
strings ["1", "2", "3"] using the map() method.

21. Check if a String is a Valid Number:


Write a function that checks if a string can be converted to a valid number
(use isNaN() ).

22. Convert false to a Number:

What will the following code output?

let value = false;


console.log(Number(value));

23. String with Leading/Trailing Spaces Conversion to a Trimmed String:

Write a function that takes a string with leading and trailing spaces and
returns it after trimming the spaces.

24. Type Conversion in Mathematical Operations:

JavaScript Task’s 31
What will the following code output?

let result = "123" - 23;


console.log(result);

25. Convert a String Representing a Date to a Date Object:

Write a function that converts a string "2023-12-31" to a Date object.

26. Handle User Input with Type Conversion:


Write a function that takes user input as a string, converts it to a number,
and logs whether it's a valid number.

27. Convert All Array Elements to Strings Using map() :


Write a function that converts all elements in an array [1, 2, 3, 4] to strings
using the map() method.

28. Convert a String to a Boolean Based on its Content:

Write a function that converts a string to a boolean. If the string is "true" , it


should be true , otherwise, it should be false .

Combined with Previous Topics (Variables, Operators, Data


Types, Arrays, Numbers, Functions, Loops)
1. Convert an Array of Mixed Types to Numbers:

Write a function that converts an array containing strings, numbers, and


booleans to all numbers.

2. Find Sum of Array of String Numbers:


Write a function that sums an array of strings representing numbers (e.g.,
["2", "4", "6"] ).

3. Handle Mixed Data Types in an Array Using Type Conversion:


Write a function that iterates through an array of mixed data types and
converts each element to a string.

4. Filter Out Non-Numeric Strings in an Array and Convert to Numbers:


Write a function that filters out non-numeric strings from an array (e.g.,
["10", "abc", "15"] ), and converts the valid ones to numbers.

5. Create an Array of Strings from an Array of Mixed Types:

JavaScript Task’s 32
Write a function that converts an array of mixed types [1, "hello", true,

null] into an array of strings ["1", "hello", "true", "null"] .

6. Convert User Input and Check for Valid Number:


Write a function that takes user input as a string, converts it to a number,
and checks if it's a valid number using isNaN() .

7. Sum Numbers in an Array After Type Conversion:


Write a function that sums all elements in an array after converting them to
numbers, even if they are strings.

8. Convert Mixed Data Types in a Loop and Print Them:


Write a function that loops through an array containing mixed data types,
converts each element to a string, and prints it.

9. Convert a Boolean to String and Concatenate with Another String:


Write a function that converts a boolean to a string and then concatenates it
with another string.

Guessing the Output of the given Code…!


Output Guessing Questions

💡 Note: Mention the reason for the answer your are giving (Mandatory).

1. Question 1:

console.log(3 + 2 + '5');

What will be the output?

2. Question 2:

let a = 1;
let b = '1';

JavaScript Task’s 33
console.log(a == b);
console.log(a === b);

What will be the output?

3. Question 3:

console.log('5' - 3);

What will be the output?

4. Question 4:

console.log('10' * '2');

What will be the output?

5. Question 5:

let x = null;
let y = undefined;
console.log(x == y);
console.log(x === y);

What will be the output?

6. Question 6:

console.log(0 == false);
console.log(0 === false);

What will be the output?

7. Question 7:

console.log(1 + true);
console.log(1 + false);

What will be the output?

8. Question 8:

JavaScript Task’s 34
let arr = [1, 2, 3];
console.log(arr + '4');

What will be the output?

9. Question 9:

console.log('1' + 2 - 1);

What will be the output?

10. Question 10:

console.log([] == ![]);

What will be the output?

11. Question 11:

let obj = { a: 1, b: 2 };
let obj2 = { a: 1, b: 2 };
console.log(obj == obj2);

What will be the output?

12. Question 12:

let a = [];
let b = [];
console.log(a == b);
console.log(a === b);

What will be the output?

13. Question 13:

console.log(!!'0');
console.log(!!0);

What will be the output?

JavaScript Task’s 35
14. Question 14:

console.log([] == false);
console.log([] == true);

What will be the output?

15. Question 15:

console.log('5' == 5);
console.log('5' === 5);

What will be the output?

16. Question 16:

let foo = 'hello';


console.log(foo.charAt(1));
console.log(foo[1]);

What will be the output?

17. Question 17:

console.log(0 || 1);
console.log(1 || 0);
console.log(0 && 1);
console.log(1 && 0);

What will be the output?

18. Question 18:

let x = 5;
let y = 10;
console.log(x > y ? 'Greater' : 'Smaller');

What will be the output?

19. Question 19:

JavaScript Task’s 36
console.log(5 + 3 + '2');
console.log('5' + 3 + 2);

What will be the output?

20. Question 20:

let obj = { a: 1, b: 2 };
console.log(obj.a);
delete obj.a;
console.log(obj.a);

What will be the output?

21. Question 21:

let arr = [1, 2, 3];


console.log(arr.length);
arr.push(4);
console.log(arr.length);

What will be the output?

22. Question 22:

let x = '10';
let y = '20';
console.log(x + y);
console.log(Number(x) + Number(y));

What will be the output?

23. Question 23:

let str = '123abc';


console.log(parseInt(str));

What will be the output?

24. Question 24:

JavaScript Task’s 37
console.log(Boolean('false'));
console.log(Boolean(''));

What will be the output?

25. Question 25:

let foo = [10, 20, 30];


console.log(foo[2]);
foo[2] = 40;
console.log(foo[2]);

What will be the output?

26. Question 26:

console.log('abc'.length);
console.log('abc'.slice(0, 2));

What will be the output?

27. Question 27:

let num = 3;
console.log(num++);
console.log(++num);

What will be the output?

28. Question 28:

let num = 3;
console.log(num--);
console.log(--num);

What will be the output?

29. Question 29:

console.log(typeof NaN);
console.log(typeof null);

JavaScript Task’s 38
console.log(typeof undefined);

What will be the output?

30. Question 30:

console.log(1 + '1' - 1);

What will be the output?

31. Question 31:

let str = '5';


console.log(str * 2);

What will be the output?

32. Question 32:

let a = 'Hello';
let b = 'World';
console.log(a + ' ' + b);

What will be the output?

33. Question 33:

let arr = [1, 2, 3];


console.log(arr.pop());
console.log(arr);

What will be the output?

34. Question 34:

let arr = [10, 20, 30];


console.log(arr.join('-'));

What will be the output?

35. Question 35:

JavaScript Task’s 39
let x = 'Hello';
let y = 'World';
console.log(x.concat(' ', y));

What will be the output?

JavaScript Task’s 40

You might also like