JS Practice Problems
JS Practice Problems
Mandatory !!! — Please watch the below two sessions - it’s mandatory to
watch them as you proceed further because the video covers all the basics
that are required for problem solving.
Session 1:
https://drive.google.com/file/d/1qMjz6miH_KfcK2i1TFkBrQrbr4HqMtaR/view?usp=share_link
Session 2:
https://drive.google.com/file/d/1QFpJRuGgamzIX2bHaiUiRFmW05_xPLDK/view?
usp=share_link
1. Create a function that takes two numbers as arguments and returns their sum.
2. Write a function that takes an integer minutes and converts it to seconds.
3. Create a function that takes a number as an argument, increments the number
by +1 and returns the result.
4. Create a function that takes the age in years and returns the age in days.
5. sniCreate a function that takes voltage and current and returns the
calculated power.
6. Write a function that returns the string "something" joined with a space " "
and the given argument a.
7. Create a function that takes two arguments. Both arguments are integers, a and
b. Return true if one of them is 10 or if their sum is 10.
8. Create a function that takes two strings as arguments and returns either true or
false depending on whether the total number of characters in the first string is
equal to the total number of characters in the second string.
9. Create a function that takes a name and returns a greeting in the form of a string.
Don't use a normal function, use an arrow function.
10. Create a function that takes an array of 10 numbers (between 0 and 9) and
returns a string of those numbers formatted as a phone number (e.g. (555) 555-
5555).
11. Create a function that returns an array of strings sorted by length in ascending
order.
Example:
sortByLength(["a", "ccc", "dddd", "bb"]) ["a", "bb", "ccc", "dddd"]
12. Create a function that takes an array of arrays with numbers. Return a new
(single) array with the largest numbers of each.
Example:
1. findLargestNums([[4, 2, 7, 1], [20, 70, 40, 90], [1, 2, 0]]) [7, 90, 2]
13. Create a function that takes an array of numbers and returns the second largest
number.
Example:
secondLargest([10, 40, 30, 20, 50]) 40
14. Create a function that takes an array of items, removes all duplicate items and
returns a new array in the same sequential order as the old array (minus
duplicates).
Example:
15. Create a function that takes an array of integers as an argument and returns a
unique number from that array. All numbers except unique ones have the same
number of occurrences in the array.
Example:
findSingleNumber([2, 2, 2, 3, 4, 4, 4]) 3
16. Create a function that takes two strings as arguments and returns the number of
times the first string (the single character) is found in the second string.
Example:
17. Create a function that takes a string and returns the number (count) of vowels
contained within it.
Example:
countVowels("Celebration") 5
18. Given a string, create a function to reverse the case. All lower-cased letters
should be upper-cased, and vice versa.
Example:
reverseCase("Happy Birthday") "hAPPY bIRTHDAY"
19. Take one integer n, loop till n and pass each value to a function, create a
function that takes one integer parameter, and multiply with 2 in every integer.
Input: n=5
Output: 2 4 6 8 10
20. Create Function that will take one parameter and return type of the data.
Input: 500
Output: Integer
Input: Coding
Output: String
Input: 4 8 2
Output: 8 is gretest
Input: n=5
Output: 120
Explanation: 5 x 4 x 3 x 2 x 1 = 120
Sort the Array using loop only(you can not use predefined function).
1
12
123
1234
12345
25. C Program to Calculate the Power of a
Number(using loop only).
Input: 9
Output: 9 is not a prime no
Input: 7
Output : 7 is a prime no
Input: 15 50
Output: Lcm of 15 and 50 is 150.
Input: i am ram
Output: 3 vowels 3 consonants.
Input: 123
Output: 321
1. Write a function that converts an object into an array, where each element
represents a key-value pair in the form of an array.
Examples :
toArray({}) []
2. Create a function that takes two numbers as arguments (num, length) and
returns an array of multiples of num until the array length reaches length.
Examples :
arrayOfMultiples(12, 10) [12, 24, 36, 48, 60, 72, 84, 96, 108, 120]
3. Create the function that takes an array with objects and returns the sum of
people's budgets.
Examples :
getBudgets([
{ name: "John", age: 21, budget: 23000 },
{ name: "Steve", age: 32, budget: 40000 },
{ name: "Martin", age: 16, budget: 2700 }
]) 65700
getBudgets([
{ name: "John", age: 21, budget: 29000 },
{ name: "Steve", age: 32, budget: 32000 },
{ name: "Martin", age: 16, budget: 1600 }
]) 62600
4. Create a function that takes an array of objects like { name: "John", notes:
[3, 5, 4]} and returns an array of objects like { name: "John", avgNote:
4 }. If a student has no notes (an empty array) then let's assume avgNote: 0.
Example :
[
{ name: "John", notes: [3, 5, 4]}
] [
{ name: "John", avgNote: 4 }
]
5. Create a function that moves all capital letters to the front of a word.
Examples :
capToFront("hApPy") "APhpy"
capToFront("moveMENT") "MENTmove"
capToFront("shOrtCAKE") "OCAKEshrt"
7. Write a function that accepts an array of strings. Return the longest string(can not
use predefined function).
Input: [5,3,5,2,1,1,7,3,5,6]
Output: [7,6,5,32,1]
Input: a + b-(9+c)=3
Output: a + b- 9+c=3
11. Write Program to remove duplicate elements in an array and sort it in Accending
order(can not use predefined function).
Input: [Z, A, P, C, A, Z , K, N, C]
Output: [A, C, K,N, P, Z]
12. If subseq's array sequence is present in the array, returns true or else returns
false.
Let arr = [5, 7, 3, 2, 2, 7,-1, 5, -3, 13, 4]
Example:
Input : Subseq1 = [7, -1, 5, -3] Output: true
Subseq2 = [7, -1, 4, -3] : false
Subseq3 = [ -1] : true
Subseq4 = [13, -3, 4, 1] : false
13. Find sum of the Unique numbers:
Example : Let arr = [1, 2, 2, 1, 3, 5, 1];
The unique numbers are 1,2, 3, 5 so the sum should be 11.
Examples :
toCamelCase("A-B-C") "ABC"
toCamelCase("the-stealth-warrior") "theStealthWarrior"
toCamelCase("The_Stealth_Warrior") "TheStealthWarrior"
2. Create a function that takes an array of strings and returns an array with only the
strings that have numbers in them. If there are no strings containing numbers,
return an empty array.
Examples :
3. Write a function that takes a list of hours and returns the total weekly salary.
● The input list hours is listed sequentially, ordered from Monday to Sunday.
● A worker earns $10 an hour for the first 8 hours.
● For every overtime hour, he earns $15.
● On weekends, the employer pays double the usual rate, regardless how
many hours were worked previously that week. For instance, 10 hours
worked on a weekday would pay 80+30 = $110, but on a weekend it
would pay 160+60 = $220.
Examples :
Examples :
parseCode("John000Doe000123") {
firstName: "John",
lastName: "Doe",
id: "123"
parseCode("michael0smith004331") {
firstName: "michael",
lastName: "smith",
id: "4331"
parseCode("Thomas00LEE0000043") {
firstName: "Thomas",
lastName: "LEE",
id: "43"
5. Create a function that takes the current day (e.g. "2022-09-30"), an array of date
objects and returns the "current streak" (i.e. number of consecutive days in a
row).
Examples :
currentStreak("2022-09-23", [
{"date": "2022-09-18"},
{"date": "2022-09-19"},
{"date": "2022-09-21"},
{"date": "2022-09-22"},
{"date": "2022-09-23"}]) 3
currentStreak("2022-09-25", [
{"date": "2022-09-16"},
{"date": "2022-09-17"},
{"date": "2022-09-21"},
{"date": "2022-09-22"},
{"date": "2022-09-23"}]) 0
6. Given a String(Note:- String Will Contain all later from A-Z except 1 letter, that letter
you need to find out) :-
Input string=“6 E @ f w 3 x y g N 1 o p Q A b c h i j # K l d m R T
U V Z”
Note:- (
Time Complexity:- O(n) means only 1 loop you can use.
without using any predefined function.
)
Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.
Input: s = "cbbd"
Output: "bb"
8. Given an unsorted array of integers nums, return the length of the longest
consecutive elements sequence.
You must write an algorithm that runs in O(n) time.
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its
length is 4.
Output: 9
9. Take an array of strings and create a dropdown using array values the values of
dropdown should be taken dynamically and if we select a value in dropdown and the
length of dropdown value is even it should show 1 in console or else if it is odd it should
0.