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

1. Write a JavaScript program to compare two objects to determine if the first one contains equivalent property values to the second one. Go to the editor

Click me to see the solution

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

Click me to see the solution

3. Write a JavaScript program to converts a comma-separated values (CSV) string to a 2D array. Go to the editor

Click me to see the solution

4. Write a JavaScript program to convert a comma-separated values (CSV) string to a 2D array of objects. The first row of the string is used as the title row. Go to the editor

Click me to see the solution

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

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. Go to the editor

Click me to see the solution

7. Write a JavaScript program to converts a specified number to an array of digits. Go to the editor

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 the filtered values. Go to the editor

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. Go to the editor

Click me to see the solution

10. Write a JavaScript program to extract out the values at the specified indexes from a specified array. Go to the editor

Click me to see the solution

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

Click me to see the solution

12. Write a JavaScript program to removes non-printable ASCII characters from a given string. Go to the editor

Click me to see the solution

13. Write a JavaScript program to convert the length of a given string in bytes. Go to the editor

Click me to see the solution

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

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 comparing rule. Go to the editor

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. Go to the editor

Click me to see the solution

17. Write a JavaScript program to split values of two given arrays into two groups. If an element in filter is truthy, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group. Go to the editor

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

25. Write a JavaScript program to create a new array out of the two supplied by creating each possible pair from the arrays. Go to the editor

Click me to see the solution

26. Write a JavaScript program that will return true if the string is y/yes or false if the string is n/no. Go to the editor

Click me to see the solution

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

Click me to see the solution

28. Write a JavaScript program to measure the time taken by a function to execute. Go to the editor

Click me to see the solution

29. Write a JavaScript program to convert a value to a safe integer. Go to the editor

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. Go to the editor

Click me to see the solution

31. Write a JavaScript program to find all elements in a given array except for the first one. Return the whole array if the array's length is 1. Go to the editor

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. Go to the editor

Click me to see the solution

33. Write a JavaScript program to get a random number in the specified range. Go to the editor

Click me to see the solution

34. Write a JavaScript program to get a random integer in the specified range. Go to the editor

Click me to see the solution

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

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. Go to the editor

Click me to see the solution

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

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. Go to the editor

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. Go to the editor

Click me to see the solution

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

Click me to see the solution

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

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. Go to the editor

Click me to see the solution

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

Click me to see the solution

44. Write a JavaScript program to remove falsey values from a given array. Go to the editor

Click me to see the solution

45. Write a JavaScript program to split values into two groups, if an element in filter is truthy, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group. Go to the editor

Click me to see the solution

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

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. Go to the editor

Click me to see the solution

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

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. Go to the editor

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. Go to the editor

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

53. Write a JavaScript program to Initialize a two dimension array of given width and height and value. Go to the editor

Click me to see the solution

54. Write a JavaScript program to initialize an array containing the numbers in the specified range where start and end are inclusive with their common difference step.Go to the editor

Click me to see the solution

55. Write a JavaScript program to Join all given URL segments together, then normalizes the resulting URL. Go to the editor

Click me to see the solution

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

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. Go to the editor

Click me to see the solution

58. Write a JavaScript program to split values into two groups according to a predicate function, which specifies which group an element in the input collection belongs to. Go to the editor

Click me to see the solution

59. Write a JavaScript program to create a function that invokes fn with a given context, optionally adding any additional supplied parameters to the beginning of the arguments. Go to the editor

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 adding any additional supplied parameters to the beginning of the arguments. Go to the editor

Click me to see the solution

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

Click me to see the solution

62. Write a JavaScript program to chain asynchronous functions. Go to the editor

Click me to see the solution

63. Write a JavaScript program to clone a given regular expression. Go to the editor

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

68. Write a JavaScript program that accepts a converging function and a list of branching functions and returns a function that applies each branching function to the arguments and the results of the branching functions are passed as arguments to the converging function. Go to the editor

Click me to see the solution

69. Write a JavaScript program to group the elements of an array based on the given function and returns the count of elements in each group. Go to the editor

Click me to see the solution

70. Write a JavaScript program to count the occurrences of a value in an array. Go to the editor

Click me to see the solution

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

Click me to see the solution

72. Write a JavaScript program to detect whether the website is being opened in a mobile device or a desktop/laptop. Go to the editor

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. Go to the editor

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. Go to the editor

Click me to see the solution

75. Write a JavaScript program to compute the new 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 best performer to worst performer (winner -> loser). Go to the editor

Click me to see the solution

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

Click me to see the solution

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

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. Go to the editor

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 returns the longest one. Go to the editor

Click me to see the solution

80. Write a JavaScript program to implement the Luhn Algorithm used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers etc. Go to the editor

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 and the same values as the provided object. Go to the editor

Click me to see the solution

82. Write a JavaScript program to map the values of an array to an object using a function, where the key-value pairs consist of the original value as the key and the mapped value. Go to the editor

Click me to see the solution

83. Write a JavaScript program to create a new string with the results of calling a provided function on every character in the calling string. Go to the editor

Click me to see the solution

84. Write a JavaScript program to create an object with the same keys as the provided object and values generated by running the provided function for each value. Go to the editor

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. Go to the editor

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. Go to the editor

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, then return the original array(sorted in descending order). Go to the editor

Click me to see the solution

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

Click me to see the solution

89. Write a JavaScript program to negates a predicate function.Go to the editor

Click me to see the solution

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

Click me to see the solution

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

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. Go to the editor

Click me to see the solution

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

Click me to see the solution

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

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. Go to the editor

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. Go to the editor

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). Go to the editor

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. Go to the editor

Click me to see the solution

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

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 and using function as the last value to specify how grouped values should be combined. Go to the editor

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. Go to the editor

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 arrays. Go to the editor

Click me to see the solution

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

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. Go to the editor

Click me to see the solution

105. Write a JavaScript program that return true if the given value is a number, false otherwise. Go to the editor

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. Go to the editor

Click me to see the solution

107. Write a JavaScript program to get all unique values (form the right side of the array) of an array, based on a provided comparator function.Go to the editor

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. Go to the editor

Click me to see the solution

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

Click me to see the solution

110. Write a JavaScript program to get every element that exists in any of the two arrays once. Go to the editor

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

113. Write a JavaScript program to unescape escaped HTML characters. Go to the editor

Click me to see the solution

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

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.Go to the editor

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). Go to the editor

Click me to see the solution

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

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). Go to the editor

Click me to see the solution

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

Click me to see the solution

120. Write a JavaScript program to convert a string to snake case. Go to the editor

Click me to see the solution

121. Write a JavaScript program to convert a value to a safe integer. Go to the editor

Click me to see the solution

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

Click me to see the solution

123. Write a JavaScript program to convert a string to kebab case. Go to the editor

Click me to see the solution

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

Click me to see the solution

125. Write a JavaScript program to convert a float-point arithmetic to the Decimal mark form and It will make a comma separated string from a number. Go to the editor

Click me to see the solution

126. Write a JavaScript program to create a specified currency formatting from a given number. Go to the editor

Click me to see the solution

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

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. Go to the editor

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. Go to the editor

Click me to see the solution

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

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 Go to the editor

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. Go to the editor

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. Go to the editor

Click me to see the solution

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

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). Go to the editor

Click me to see the solution

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

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. Do not mutate the original array, but returns a new array instead. Go to the editor

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. Go to the editor

Click me to see the solution

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

Click me to see the solution

140. Write a JavaScript program to get the highest index at which value should be inserted into array in order to maintain its sort order, based on a provided iterator function. Go to the editor

Click me to see the solution

141. Write a JavaScript program to get the highest index at which value should be inserted into array in order to maintain its sort order. Go to the editor

Click me to see the solution

142. Write a JavaScript program to get the lowest index at which value should be inserted into array in order to maintain its sort order.Go to the editor

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

Click me to see the solution

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

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.

JavaScript: Tips of the Day

Creates an object composed of the properties the given function returns truthy for. The function is invoked with two arguments: (value, key)

Example:

const tips_composed = (obj, fn) =>
  Object.keys(obj)
    .filter(k => fn(obj[k], k))
    .reduce((acc, key) => ((acc[key] = obj[key]), acc), {});

console.log(tips_composed({ p: 2, q: '4', r: 6 }, x => typeof x === 'number'));

Output:

[object Object] {
  p: 2,
  r: 6
}