Back to Exercises
Core JavaScript (Basics)
20 Exercises Available
Variables, operators, type conversion, template strings — build a strong foundation.
1.Easy
Safe Number Input (string → number) + Sum
Create a function `sum(a, b)` that accepts inputs as string or number. The function should strictly convert to number and return the sum. Return `null` for invalid input.
Example:
- sum("10", "5") → 15
- sum("10a", 5) → null
Sample Output:
sum("10","5") => 15
sum("10a",5) => nullView Solution
2.Easy
Price After Discount (round to 2 decimals)
Create a function `finalPrice(price, discountPercent)`. Apply discount and return the final price. Output should be rounded to 2 decimals.
Example:
- finalPrice(999, 12.5) → 874.13
Sample Output:
finalPrice(999, 12.5) => 874.13 finalPrice(1000, 20) => 800
View Solution
3.Easy
Template String: User Greeting
Create a function `greet(name, city)` that returns a sentence with trimmed name/city:
`Hi <name>! Welcome to <city>.`
Note: Use `Guest` if name/city is blank.
Sample Output:
greet(" Alice ", "Mumbai") => "Hi Alice! Welcome to Mumbai."
greet(null, "") => "Hi Guest! Welcome to Guest."View Solution
4.Easy
Clamp Number in Range (min/max)
Create a function `clamp(n, min, max)` that fixes a number within a range.
Example:
- clamp(120, 0, 100) → 100
- clamp(-5, 0, 100) → 0
Sample Output:
clamp(120, 0, 100) => 100 clamp(-5, 0, 100) => 0 clamp(50, 0, 100) => 50
View Solution
5.Medium
Truthy/Falsy: Is Field Filled?
Function `isFilled(value)` banao jo true tab return kare jab value meaningful ho:
- string: trimmed length > 0
- number: finite
- boolean: true
- others: false
Sample Output:
isFilled("hello") => true
isFilled(" ") => false
isFilled(5) => true
isFilled(NaN) => falseView Solution
6.Medium
Parse Integer Safely (with radix)
Create a function `parseIntSafe(str, radix=10)` that converts a string to integer. Return null for invalid input. Support radix parameter.
Example:
- parseIntSafe("123") → 123
- parseIntSafe("0xFF", 16) → 255
- parseIntSafe("abc") → null
Sample Output:
parseIntSafe("123") => 123
parseIntSafe("0xFF", 16) => 255
parseIntSafe("1010", 2) => 10
parseIntSafe("abc") => nullView Solution
7.Medium
Format Currency (Indian Format)
Create a function `formatCurrency(amount)` that converts a number to Indian currency format.
Example: 1234567 → "12,34,567"
Note: Indian numbering system uses lakhs/crores.
Sample Output:
formatCurrency(1234567) => "12,34,567" formatCurrency(10000000) => "1,00,00,000" formatCurrency(-5000) => "5,000"
View Solution
8.Easy
Generate Random String (alphanumeric)
Create a function `randomString(length=8)` that generates a random alphanumeric string.
Example: randomString(6) → "a3B9x2"
Sample Output:
randomString(6) => "a3B9x2" (random) randomString(10) => "Xy7mN2pQ9k" (random) randomString(4) => "K9mP" (random)
View Solution
9.Medium
Check if Number is Prime
Create a function `isPrime(n)` that checks if a number is prime.
Example:
- isPrime(7) → true
- isPrime(10) → false
Sample Output:
isPrime(7) => true isPrime(10) => false isPrime(2) => true isPrime(1) => false
View Solution
10.Medium
Calculate Age from Date of Birth
Create a function `calculateAge(dob)` that calculates age from date of birth.
Example: calculateAge("1990-05-15") → 34 (assuming current year is 2024)
Sample Output:
calculateAge("1990-05-15") => 34 (if current date is 2024-06-01)
calculateAge("2000-12-25") => 23 (if current date is 2024-01-01)View Solution
11.Hard
Deep Clone Object (without JSON)
Create a function `deepClone(obj)` that deeply clones an object. Do not use JSON methods.
Example: deepClone({a: 1, b: {c: 2}}) → {a: 1, b: {c: 2}} (new object)
Sample Output:
deepClone({a: 1, b: {c: 2}}) => {a: 1, b: {c: 2}} (new object)
const obj = {x: 1}; const cloned = deepClone(obj); obj.x = 2; cloned.x => 1View Solution
12.Hard
Throttle Function (rate limiting)
Create a function `throttle(fn, delay)` that throttles a function. If multiple calls occur within delay, only the first call executes.
Example: Throttled function with 500ms delay, 3 rapid calls → only 1st call executes
Sample Output:
const throttled = throttle(() => console.log("called"), 500);
throttled(); throttled(); throttled();
// Only first call executes, others ignored within 500msView Solution
13.Hard
Check if Two Objects are Equal (deep)
Create a function `deepEqual(a, b)` that deeply compares if two objects are equal.
Example: deepEqual({a: 1, b: {c: 2}}, {a: 1, b: {c: 2}}) → true
Sample Output:
deepEqual({a: 1, b: {c: 2}}, {a: 1, b: {c: 2}}) => true
deepEqual({a: 1}, {a: 2}) => false
deepEqual({x: 1, y: 2}, {x: 1}) => falseView Solution
14.Easy
Format Phone Number (Indian Format)
Create a function `formatPhone(phone)` that converts a 10-digit number to Indian format.
Example: formatPhone("9876543210") → "+91 98765 43210"
Sample Output:
formatPhone("9876543210") => "+91 98765 43210"
formatPhone("+91-98765-43210") => "+91 98765 43210"View Solution
15.Easy
Calculate Percentage
Create a function `percentage(value, total)` that calculates percentage. Round the result to 2 decimals.
Example: percentage(25, 200) → 12.5
Sample Output:
percentage(25, 200) => 12.5 percentage(75, 300) => 25 percentage(1, 3) => 33.33
View Solution
16.Medium
Generate UUID v4 (simple)
Create a function `generateUUID()` that generates a UUID v4 format string.
Example: generateUUID() → "550e8400-e29b-41d4-a716-446655440000" (format)
Sample Output:
generateUUID() => "550e8400-e29b-41d4-a716-446655440000" (format, random) generateUUID() => "a1b2c3d4-e5f6-4789-a012-b3c4d5e6f789" (format, random)
View Solution
17.Medium
Check if String is Valid Email
Create a function `isValidEmail(email)` that validates email using regex.
Example:
- isValidEmail("test@example.com") → true
- isValidEmail("invalid") → false
Sample Output:
isValidEmail("test@example.com") => true
isValidEmail("invalid") => false
isValidEmail("user@domain.co.in") => trueView Solution
18.Medium
Convert Seconds to Human Readable Time
Create a function `formatDuration(seconds)` that converts seconds to "Xh Ym Zs" format.
Example: formatDuration(3665) → "1h 1m 5s"
Sample Output:
formatDuration(3665) => "1h 1m 5s" formatDuration(125) => "2m 5s" formatDuration(45) => "45s"
View Solution
19.Easy
Find Maximum Number in Array
Create a function `findMax(arr)` that finds the maximum number in an array. Return null for empty array.
Example: findMax([3, 7, 2, 9, 1]) → 9
Sample Output:
findMax([3, 7, 2, 9, 1]) => 9 findMax([-5, -2, -10]) => -2 findMax([]) => null
View Solution
20.Easy
Check if Number is Even or Odd
Create functions `isEven(n)` and `isOdd(n)` that check if a number is even or odd.
Example:
- isEven(4) → true
- isOdd(5) → true
Sample Output:
isEven(4) => true isOdd(5) => true isEven(3) => false isOdd(2) => false
View Solution