100 JS Functions

Beginner

1. minutesToHours

2. averageOf4Numbers

3. concat3

4. max5

5. getMonthsNeededToWait

6. getGasolineAmount

7. lastNRevert

8. getBusinessAddress

9. getUserObject

10. canDriveCar

11. areAllNumbersEven

12. getBiggestNumberInArrays

13. getLongestString

14. everyNPositions

15. doubleNumbers

16. mostRepetitions

17. getMillisecondsBetween

18. getMonthOfTheYear

19. addDays

20. getDevelopers

21. extractElementsBetweenPositions

22. isSorted

23. halfAndHalf

24. isSameDay

25. getMaxMovingDistance

Intermediate

26. arrayToObject

27. pickFields

28. getHighestPaidEmployee

29. flipObject

30. diffArrays

31. countPageViews

32. linkedNumbersSum

33. getMissingContacts

34. removeFirstAndLast

35. biggestPowerOf2

36. areValuesUnique

37. fetchGitHubName

38. rotateArray

39. getDaysInMonth

40. formatDateTime

41. toDecimal

42. compareSets

43. groupBirthdays

44. diffReactions

45. rgbToHex

46. timeAgo

47. customArraySort

48. moveItems

49. isValidPassword

50. mergeSortedArrays

51. ascendingSplit

52. findUniqueNumber

53. parseQueryParams

54. simpleCompression

55. partitionArray

56. findFreeCalendarSpots

57. mergeIntervals

58. simpleURLParser

59. pickNested

60. fetchNamesOfAllPublicRepos

61. getPaginatedData

62. getCheckPassword

63. getAdd5

64. getAddN

65. fetchClosedPullRequests

66. fetchBranchNames

67. searchMessages

68. objectToMap

69. createQueue

70. createStack

71. isSameWeek

72. bfsTraversal

73. dfsTraversal

74. getDoubleN

75. deepCopy

Advanced

76. uniqBy

77. flow

78. createCounter

79. createPromise

80. reverseForEach

81. checkSettlesInTime

82. sorted

83. groupBy

84. createLinkedList

85. promiseOrder

86. isDeepEqual

87. delayResolve

88. classInheritance

89. customBind

90. todoList

91. reverseReduce

92. withCount

93. maxInvocations

94. createObservable

95. createPubSub

96. cacheGetResult

97. currySum

98. getCurry

99. throttle

100. debounce

94.createObservable

Write a function named createObservable that returns an object implementing the Observer pattern.

The object - let's call it observable - should have the following properties:

  • subscribe - a method used by observers to subscribe to the observable. It should accept 1 parameter:

    • callback - a function that will be called when we want to notify the observers
  • unsubscribe - a method used by observers to unsubscribe from the observable. It should receive the same callback sent to the subscribe function as parameter

  • notify - a method used to notify the observers. It should accept 1 parameter:

    • data - any data that should be passed as parameter to the observers' callback functions

Example 1

class User {
  name;
  weatherObservable;

  constructor(name, weatherObservable) {
    this.name = name;
    this.weatherObservable = weatherObservable;
    this.weatherObservable.subscribe(this.onWeatherChange);
  }

  onWeatherChange = (weatherData) => {
    console.log(`${this.name} received weather updates: `, weatherData);
  }

  stopListening() {
    this.weatherObservable.unsubscribe(this.onWeatherChange);
  }
}

const weatherObservable = createObservable();
const bob = new User("Bob", weatherObservable);

/**
 * Should print:
 * Bob received weather updates: { temperature: 20, humidity: 80 }
 */
weatherObservable.notify({ temperature: 20, humidity: 80 });

/**
 * Should print:
 * Bob received weather updates:  { temperature: 25, humidity: 75 }
 */
weatherObservable.notify({ temperature: 25, humidity: 75 });

bob.stopListening();

// Should not print anything, because the user is no longer
// subscribed to the weatherObservable observable.
weatherObservable.notify({ temperature: 22, humidity: 68 });

You'll see test results here!