w3resource

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.

Click me to see the solution

2. Write a JavaScript program to copy a string to the clipboard.

Click me to see the solution

3. Write a JavaScript program to convert a comma-separated value (CSV) string to a 2D array.

Click me to see the solution

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.

Click me to see the solution

5. Write a JavaScript program to convert an array of objects to a comma-separated value (CSV) string that contains only the columns specified.

Click me to see the solution

6. Write a JavaScript program to target a given value in a nested JSON object based on the given key.

Click me to see the solution

7. Write a JavaScript program to convert a specified number into an array of digits.

Click me to see the solution

8. Write a JavaScript program to filter out the specified values from a specified array. Return the original array without filtered values.

Click me to see the solution

9. Write a JavaScript program to combine the numbers of a given array into an array containing all combinations.

Click me to see the solution

10. Write a JavaScript program to extract values at specified indexes from a specified array.

Click me to see the solution

11. Write a JavaScript program to generate a random hexadecimal color code.

Click me to see the solution

12. Write a JavaScript program to remove non-printable ASCII characters from a given string.

Click me to see the solution

13. Write a JavaScript program to convert a given string's length to bytes.

Click me to see the solution

14. Write a JavaScript program to replace multiple object keys' names with the values provided.

Click me to see the solution

15. Write a JavaScript program to return the minimum-maximum value of an array, after applying the provided function to set a comparing rule.

Click me to see the solution

16. Write a JavaScript function that returns true if the provided predicate function returns true for all elements in a collection, false otherwise.

Click me to see the solution

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.

Click me to see the solution

18. Write a JavaScript program to remove specified elements from the left of a given array of elements.

Click me to see the solution

19. Write a JavaScript program to remove specified elements from the right of a given array of elements.

Click me to see the solution

20. Write a JavaScript program to extend a 3-digit color code to a 6-digit color code.

Click me to see the solution

21. Write a JavaScript program to get every nth element in a given array.

Click me to see the solution

22. Write a JavaScript program to filter out non-unique values in an array.

Click me to see the solution

23. Write a JavaScript program to filter out non-unique values in an array, based on a provided comparator function.

Click me to see the solution

24. Write a JavaScript program to dcapitalize the first letter of a string.

Click me to see the solution

25. Write a JavaScript program to create an array out of the arrays by creating each possible pair from the arrays.

Click me to see the solution

26. Write a JavaScript program that returns true if the string is y/yes or false if the string is n/no.

Click me to see the solution

27. Write a JavaScript program to find every element in any of the two given arrays at once, using the provided comparator function.

Click me to see the solution

28. Write a JavaScript program to measure the time a function to execute.

Click me to see the solution

29. Write a JavaScript program to convert a value to a safe integer.

Click me to see the solution

30. Write a JavaScript program to filter out the element(s) of a given array that have one of the specified values.

Click me to see the solution

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.

Click me to see the solution

32. Write a JavaScript program to get the sum of a given array, after mapping each element to a value using the provided function.

Click me to see the solution

33. Write a JavaScript program to generate a random number in the specified range.

Click me to see the solution

34. Write a JavaScript program to generate a random integer in the specified range.

Click me to see the solution

35. Write a JavaScript program to get an array of given n random integers in the specified range.

Click me to see the solution

36. Write a JavaScript program to create a function that invokes each provided function with the arguments it receives and returns the results.

Click me to see the solution

37. Write a JavaScript program to get a sorted array of objects ordered by properties and orders.

Click me to see the solution

38. Write a JavaScript program to pad a string on both sides with the specified character, if it's shorter than the specified length.

Click me to see the solution

39. Write a JavaScript program to remove the key-value pairs corresponding to the given keys from an object.

Click me to see the solution

40. Write a JavaScript program to create an array of key-value pair arrays from a given object.

Click me to see the solution

41. Write a JavaScript program to create an object from the given key-value pairs.

Click me to see the solution

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.

Click me to see the solution

43. Write a JavaScript program to change a function that accepts an array into a variadic function.

Click me to see the solution

44. Write a JavaScript program to remove false values from a given array.

Click me to see the solution

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.

Click me to see the solution

46. Write a JavaScript program to curry (curries) a function.

Click me to see the solution

47. Write a JavaScript program to perform a deep comparison between two values to determine if they are equivalent.

Click me to see the solution

48. Write a JavaScript program to get an array of function property names from the own (and optionally inherited) enumerable properties of an object.

Click me to see the solution

49. Write a JavaScript program to retrieve a set of properties indicated by the given selectors from an object.

Click me to see the solution

50. Write a JavaScript program to convert an integer to a suffixed string, adding am or pm based on its value.

Click me to see the solution

51. Write a JavaScript program to get an object containing the current URL parameters.

Click me to see the solution

52. Write a JavaScript program to group the elements of a given array based on the given function.

Click me to see the solution

53. Write a JavaScript program to initialize a two-dimensional array of given size and value.

Click me to see the solution

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.

Click me to see the solution

55. Write a JavaScript program to join all given URL segments together, then normalize the resulting URL.

Click me to see the solution

56. Write a JavaScript program to check whether all elements in a given array are equal or not.

Click me to see the solution

57. Write a JavaScript program to compute the average of an array, after mapping each element to a value using the provided function.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

61. Write a JavaScript program to cast the provided value as an array if it's not one.

Click me to see the solution

62. Write a JavaScript program to chain asynchronous functions.

Click me to see the solution

63. Write a JavaScript program to clone a given regular expression.

Click me to see the solution

64. Write a JavaScript program to get the first non-null / undefined argument.

Click me to see the solution

65. Write a JavaScript program to add special characters to text to print in color on the console (combined with console.log()).

Click me to see the solution

66. Write a JavaScript program to perform right-to-left function composition.

Click me to see the solution

67. Write a JavaScript program to perform left-to-right function composition.

Click me to see the solution

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.

Click me to see the solution

69. Write a JavaScript program to group array elements based on the given function. It return the count of elements in each group.

Click me to see the solution

70. Write a JavaScript program to count a value in an array.

Click me to see the solution

71. Write a JavaScript program to create a deep clone of an object.

Click me to see the solution

72. Write a JavaScript program to detect whether the website is opened on a mobile device or a desktop/laptop.

Click me to see the solution

73. Write a JavaScript program to return the difference between two arrays, after applying the provided function to each array element of both.

Click me to see the solution

74. Write a JavaScript program to filter out all values from an array for which the comparator function does not return true.

Click me to see the solution

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

Click me to see the solution

76. Write a JavaScript program to execute a provided function once for each array element, starting with the array's last element.

Click me to see the solution

77. Write a JavaScript program to iterate over all the properties of an object, running a callback for each one.

Click me to see the solution

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.

Click me to see the solution

79. Write a JavaScript program to take any number of iterable objects or objects with a length property and return the longest one.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

85. Write a JavaScript program to replace all but the last number of characters with the specified mask character.

Click me to see the solution

86. Write a JavaScript program to get the maximum value of an array, after mapping each element to a value using the provided function.

Click me to see the solution

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

Click me to see the solution

88. Write a JavaScript program to get the median of an array of numbers.

Click me to see the solution

89. Write a JavaScript program to negates a predicate function.

Click me to see the solution

90. Write a JavaScript program to nest a given flat array of objects linked to one another recursively.

Click me to see the solution

91. Write a JavaScript program that returns true if the provided predicate function returns false for all elements in a collection, false otherwise.

Click me to see the solution

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.

Click me to see the solution

93. Write a JavaScript program to remove an event listener from an element.

Click me to see the solution

94. Write a JavaScript program to move the specified amount of elements to the end of the array.

Click me to see the solution

95. Write a JavaScript program to add an event listener to an element with the ability to use event delegation.

Click me to see the solution

96. Write a JavaScript program to pick the key-value pairs corresponding to the given keys from an object.

Click me to see the solution

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

Click me to see the solution

98. Write a JavaScript program to filter an array of objects based on a condition while also filtering out unspecified keys.

Click me to see the solution

99. Write a JavaScript program to hash a given input string into a whole number.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

102. Write a JavaScript program to create an array of elements, grouped based on the position in the original array.

Click me to see the solution

103. Write a JavaScript program to convert a given string into an array of words.

Click me to see the solution

104. Write a JavaScript program to test a value, x, against a predicate function. If true, return fn(x). Else, return x.

Click me to see the solution

105. Write a JavaScript program that returns true if the given value is a number, false otherwise.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

108. Write a JavaScript program to get all unique values of an array, based on a provided comparator function.

Click me to see the solution

109. Write a JavaScript program to get the nth element of a given array.

Click me to see the solution

110. Write a JavaScript program to get every element in any of the two arrays at once.

Click me to see the solution

111. Write a JavaScript program to build an array, using an iterator function and an initial seed value.

Click me to see the solution

112. Write a JavaScript program to unflatten an object with the paths for keys.

Click me to see the solution

113. Write a JavaScript program to unescape escaped HTML characters.

Click me to see the solution

114. Write a JavaScript program to uncurry a function up to depth n.

Click me to see the solution

115. Write a JavaScript program to create a function that accepts up to one argument, ignoring any additional arguments.

Click me to see the solution

116. Write a JavaScript program to check if the predicate (second argument) is truthy on all elements of a collection (first argument).

Click me to see the solution

117. Write a JavaScript program to truncate a string up to a specified length.

Click me to see the solution

118. Write a JavaScript program to apply a function against an accumulator and each key in the object (from left to right).

Click me to see the solution

119. Write a JavaScript program to create tomorrow's date in a string representation.

Click me to see the solution

120. Write a JavaScript program to convert a string to snake case.

Click me to see the solution

121. Write a JavaScript program to convert a value to a safe integer.

Click me to see the solution

122. Write a JavaScript program to add an ordinal suffix to a number.

Click me to see the solution

123. Write a JavaScript program to convert a string to kebab case.

Click me to see the solution

124. Write a JavaScript program to reduce a given Array-like into a value hash (keyed data store).

Click me to see the solution

125. Write a JavaScript program that converts float-point arithmetic to decimal form, and creates a comma separated string from a number.

Click me to see the solution

126. Write a JavaScript program to create a specified currency format from a given number.

Click me to see the solution

127. Write a JavaScript program to Iterate over a callback n times.

Click me to see the solution

128. Write a JavaScript program to get removed elements of a given array until the passed function returns true.

Click me to see the solution

129. Write a JavaScript program to get removed elements from the end of a given array until the passed function returns true.

Click me to see the solution

130. Write a JavaScript program to remove n elements from the end of a given array.

Click me to see the solution

131. Write a JavaScript program to get an array with n elements removed from the beginning from a given array

Click me to see the solution

132. Write a JavaScript program to get the symmetric difference between two given arrays, using a provided function as a comparator.

Click me to see the solution

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.

Click me to see the solution

134. Write a JavaScript program to get the symmetric difference between two given arrays.

Click me to see the solution

135. Write a JavaScript program to get the sum of the powers of all the numbers from start to end (both inclusive).

Click me to see the solution

136. Write a JavaScript program to generate all permutations of a string (contains duplicates).

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

139. Write a JavaScript program to split a multiline string into an array of lines.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

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.

Click me to see the solution

143. Write a JavaScript program to sort the characters of a string Alphabetically.

Click me to see the solution

144. Write a JavaScript program to get an array of elements that appear in both arrays.

Click me to see the solution

145. Write a JavaScript program to randomize the order of array values, returning an updated array.

Click me to see the solution

146. Write a JavaScript program to create a shallow clone of an object.

Click me to see the solution

147. Write a JavaScript program to serialize a cookie name-value pair into a Set-Cookie header string.

Click me to see the solution

148. Write a JavaScript program to hash the input string into a whole number.

Click me to see the solution

149. Write a JavaScript program to get a random element from an array.

Click me to see the solution

150. Write a JavaScript program to run a given array of promises in series.

Click me to see the solution

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.



Follow us on Facebook and Twitter for latest update.