JavaScript fundamental - Exercises, Practice, Solution
JavaScript fundamental [150 exercises with solution]
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Write a JavaScript program to compare two objects to determine if the first contains equivalent property values to the second one.
2. Write a JavaScript program to copy a string to the clipboard.
3. Write a JavaScript program to convert a comma-separated value (CSV) string to a 2D array.
4. Write a JavaScript program to convert a comma-separated value (CSV) string to a 2D array of objects. The first row of the string is used as the title row.
5. Write a JavaScript program to convert an array of objects to a comma-separated value (CSV) string that contains only the columns specified.
6. Write a JavaScript program to target a given value in a nested JSON object based on the given key.
7. Write a JavaScript program to convert a specified number into an array of digits.
8. Write a JavaScript program to filter out the specified values from a specified array. Return the original array without filtered values.
9. Write a JavaScript program to combine the numbers of a given array into an array containing all combinations.
10. Write a JavaScript program to extract values at specified indexes from a specified array.
11. Write a JavaScript program to generate a random hexadecimal color code.
12. Write a JavaScript program to remove non-printable ASCII characters from a given string.
13. Write a JavaScript program to convert a given string's length to bytes.
14. Write a JavaScript program to replace multiple object keys' names with the values provided.
15. Write a JavaScript program to return the minimum-maximum value of an array, after applying the provided function to set a comparing rule.
16. Write a JavaScript function that returns true if the provided predicate function returns true for all elements in a collection, false otherwise.
17. Write a JavaScript program to split the values of two given arrays into two groups. If an element in the filter is true, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.
18. Write a JavaScript program to remove specified elements from the left of a given array of elements.
19. Write a JavaScript program to remove specified elements from the right of a given array of elements.
20. Write a JavaScript program to extend a 3-digit color code to a 6-digit color code.
21. Write a JavaScript program to get every nth element in a given array.
22. Write a JavaScript program to filter out non-unique values in an array.
23. Write a JavaScript program to filter out non-unique values in an array, based on a provided comparator function.
24. Write a JavaScript program to dcapitalize the first letter of a string.
25. Write a JavaScript program to create an array out of the arrays by creating each possible pair from the arrays.
26. Write a JavaScript program that returns true if the string is y/yes or false if the string is n/no.
27. Write a JavaScript program to find every element in any of the two given arrays at once, using the provided comparator function.
28. Write a JavaScript program to measure the time a function to execute.
29. Write a JavaScript program to convert a value to a safe integer.
30. Write a JavaScript program to filter out the element(s) of a given array that have one of the specified values.
31. Write a JavaScript program to find all elements in a given array except the first one. Return the whole array if its length is 1.
32. Write a JavaScript program to get the sum of a given array, after mapping each element to a value using the provided function.
33. Write a JavaScript program to generate a random number in the specified range.
34. Write a JavaScript program to generate a random integer in the specified range.
35. Write a JavaScript program to get an array of given n random integers in the specified range.
36. Write a JavaScript program to create a function that invokes each provided function with the arguments it receives and returns the results.
37. Write a JavaScript program to get a sorted array of objects ordered by properties and orders.
38. Write a JavaScript program to pad a string on both sides with the specified character, if it's shorter than the specified length.
39. Write a JavaScript program to remove the key-value pairs corresponding to the given keys from an object.
40. Write a JavaScript program to create an array of key-value pair arrays from a given object.
41. Write a JavaScript program to create an object from the given key-value pairs.
42. Write a JavaScript program to get a customized coalesce function that returns the first argument that returns true from the provided argument validation function.
43. Write a JavaScript program to change a function that accepts an array into a variadic function.
44. Write a JavaScript program to remove false values from a given array.
45. Write a JavaScript program to split values into two groups. If an element in the filter is true, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.
46. Write a JavaScript program to curry (curries) a function.
47. Write a JavaScript program to perform a deep comparison between two values to determine if they are equivalent.
48. Write a JavaScript program to get an array of function property names from the own (and optionally inherited) enumerable properties of an object.
49. Write a JavaScript program to retrieve a set of properties indicated by the given selectors from an object.
50. Write a JavaScript program to convert an integer to a suffixed string, adding am or pm based on its value.
51. Write a JavaScript program to get an object containing the current URL parameters.
52. Write a JavaScript program to group the elements of a given array based on the given function.
53. Write a JavaScript program to initialize a two-dimensional array of given size and value.
54. Write a JavaScript program to initialize an array containing numbers in the specified range. Start and end are inclusive of their common point of difference.
55. Write a JavaScript program to join all given URL segments together, then normalize the resulting URL.
56. Write a JavaScript program to check whether all elements in a given array are equal or not.
57. Write a JavaScript program to compute the average of an array, after mapping each element to a value using the provided function.
58. Write a JavaScript program to split values into two groups according to a predicate function. This specifies which group an element in the input collection belongs to.
59. Write a JavaScript program to create a function that invokes fn in a given context. Optionally add any additional variables to the arguments beginning.
60. Write a JavaScript program to create a function that invokes the method at a given key of an object. Optionally, add any parameters that are supplied to the beginning of the arguments.
61. Write a JavaScript program to cast the provided value as an array if it's not one.
62. Write a JavaScript program to chain asynchronous functions.
63. Write a JavaScript program to clone a given regular expression.
64. Write a JavaScript program to get the first non-null / undefined argument.
65. Write a JavaScript program to add special characters to text to print in color on the console (combined with console.log()).
66. Write a JavaScript program to perform right-to-left function composition.
67. Write a JavaScript program to perform left-to-right function composition.
68. Write a JavaScript program that accepts a converging function and a list of branching functions. It returns a function that applies each branching function to the arguments. The results of the branching functions are passed as arguments to the converging function.
69. Write a JavaScript program to group array elements based on the given function. It return the count of elements in each group.
70. Write a JavaScript program to count a value in an array.
71. Write a JavaScript program to create a deep clone of an object.
72. Write a JavaScript program to detect whether the website is opened on a mobile device or a desktop/laptop.
73. Write a JavaScript program to return the difference between two arrays, after applying the provided function to each array element of both.
74. Write a JavaScript program to filter out all values from an array for which the comparator function does not return true.
75. Write a JavaScript program to compute the updated ratings between two or more opponents using the Elo rating system. It takes an array of pre-ratings and returns an array containing post-ratings. The array should be ordered from top to bottom (winner -> loser).
76. Write a JavaScript program to execute a provided function once for each array element, starting with the array's last element.
77. Write a JavaScript program to iterate over all the properties of an object, running a callback for each one.
78. Write a JavaScript program to invert the key-value pairs of an object, without mutating it. The corresponding inverted value of each inverted key is an array of keys responsible for generating the inverted value. If a function is supplied, it is applied to each inverted key.
79. Write a JavaScript program to take any number of iterable objects or objects with a length property and return the longest one.
80. Write a JavaScript program to implement the Luhn Algorithm used to validate identification numbers. For example, credit card numbers, IMEI numbers, National Provider Identifier numbers etc.
81. Write a JavaScript program to create an object with keys generated by running the provided function for each key. The object will have the same values as the provided object.
82. Write a JavaScript program to map array values to an object using a function. The key-value pairs consist of the original value as the key and the mapped value.
83. Write a JavaScript program to create an updated string with the results of calling a provided function on every character in the called string.
84. Write a JavaScript program to create an object with the same keys as the provided object. It will also generate values generated by running the provided function for each value.
85. Write a JavaScript program to replace all but the last number of characters with the specified mask character.
86. Write a JavaScript program to get the maximum value of an array, after mapping each element to a value using the provided function.
87. Write a JavaScript program to get the n maximum elements from the provided array. If n is greater than or equal to the provided array's length, return the original array (sorted in descending order).
88. Write a JavaScript program to get the median of an array of numbers.
89. Write a JavaScript program to negates a predicate function.
90. Write a JavaScript program to nest a given flat array of objects linked to one another recursively.
91. Write a JavaScript program that returns true if the provided predicate function returns false for all elements in a collection, false otherwise.
92. Write a JavaScript program to create a function that gets the argument at index n. If n is negative, the nth argument from the end is returned.
93. Write a JavaScript program to remove an event listener from an element.
94. Write a JavaScript program to move the specified amount of elements to the end of the array.
95. Write a JavaScript program to add an event listener to an element with the ability to use event delegation.
96. Write a JavaScript program to pick the key-value pairs corresponding to the given keys from an object.
97. Write a JavaScript program to create an object composed of the properties the given function returns truthy for. The function is invoked with two arguments: (value, key).
98. Write a JavaScript program to filter an array of objects based on a condition while also filtering out unspecified keys.
99. Write a JavaScript program to hash a given input string into a whole number.
100. Write a JavaScript program to create an array of elements, grouped based on the position in the original arrays. Use function as the last value to specify how grouped values should be combined.
101. Write a JavaScript program to return the object associating the properties to the values of a given array of valid property identifiers and an array of values.
102. Write a JavaScript program to create an array of elements, grouped based on the position in the original array.
103. Write a JavaScript program to convert a given string into an array of words.
104. Write a JavaScript program to test a value, x, against a predicate function. If true, return fn(x). Else, return x.
105. Write a JavaScript program that returns true if the given value is a number, false otherwise.
106. Write a JavaScript program to create an array of elements, ungrouping the elements in an array produced by zip and applying the provided function.
107. Write a JavaScript program to get all distinct values (from the right side of the array) of an array, based on a provided comparator function.
108. Write a JavaScript program to get all unique values of an array, based on a provided comparator function.
109. Write a JavaScript program to get the nth element of a given array.
110. Write a JavaScript program to get every element in any of the two arrays at once.
111. Write a JavaScript program to build an array, using an iterator function and an initial seed value.
112. Write a JavaScript program to unflatten an object with the paths for keys.
113. Write a JavaScript program to unescape escaped HTML characters.
114. Write a JavaScript program to uncurry a function up to depth n.
115. Write a JavaScript program to create a function that accepts up to one argument, ignoring any additional arguments.
116. Write a JavaScript program to check if the predicate (second argument) is truthy on all elements of a collection (first argument).
117. Write a JavaScript program to truncate a string up to a specified length.
118. Write a JavaScript program to apply a function against an accumulator and each key in the object (from left to right).
119. Write a JavaScript program to create tomorrow's date in a string representation.
120. Write a JavaScript program to convert a string to snake case.
121. Write a JavaScript program to convert a value to a safe integer.
122. Write a JavaScript program to add an ordinal suffix to a number.
123. Write a JavaScript program to convert a string to kebab case.
124. Write a JavaScript program to reduce a given Array-like into a value hash (keyed data store).
125. Write a JavaScript program that converts float-point arithmetic to decimal form, and creates a comma separated string from a number.
126. Write a JavaScript program to create a specified currency format from a given number.
127. Write a JavaScript program to Iterate over a callback n times.
128. Write a JavaScript program to get removed elements of a given array until the passed function returns true.
129. Write a JavaScript program to get removed elements from the end of a given array until the passed function returns true.
130. Write a JavaScript program to remove n elements from the end of a given array.
131. Write a JavaScript program to get an array with n elements removed from the beginning from a given array
132. Write a JavaScript program to get the symmetric difference between two given arrays, using a provided function as a comparator.
133. Write a JavaScript program to get the symmetric difference between two given arrays, after applying the provided function to each array element of both.
134. Write a JavaScript program to get the symmetric difference between two given arrays.
135. Write a JavaScript program to get the sum of the powers of all the numbers from start to end (both inclusive).
136. Write a JavaScript program to generate all permutations of a string (contains duplicates).
137. Write a JavaScript program to perform stable sorting of an array, preserving the initial indexes of items when their values are the same. Returns a new array instead of mutating the original array.
138. Write a JavaScript program that takes a variadic function and returns a closure that accepts an array of arguments to map to the inputs of the function.
139. Write a JavaScript program to split a multiline string into an array of lines.
140. Write a JavaScript program to get the highest index at which value should be inserted into an array in order to maintain its sort order. This is based on a provided iterator function.
141. Write a JavaScript program to get the highest index at which value should be inserted into an array in order to maintain its sort order.
142. Write a JavaScript program to get the lowest index at which values should be inserted into an array in order to maintain its sorting order.
143. Write a JavaScript program to sort the characters of a string Alphabetically.
144. Write a JavaScript program to get an array of elements that appear in both arrays.
145. Write a JavaScript program to randomize the order of array values, returning an updated array.
146. Write a JavaScript program to create a shallow clone of an object.
147. Write a JavaScript program to serialize a cookie name-value pair into a Set-Cookie header string.
148. Write a JavaScript program to hash the input string into a whole number.
149. Write a JavaScript program to get a random element from an array.
150. Write a JavaScript program to run a given array of promises in series.
More to Come !
* To run the code mouse over on Result panel and click on 'RERUN' button.*
Live Demo:
See the Pen javascript-common-editor by w3resource (@w3resource) on CodePen.
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/javascript-exercises/fundamental/index.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics