Leetcode (Javascript pattern)

0.0(0)
studied byStudied by 0 people
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/4

flashcard set

Earn XP

Description and Tags

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

5 Terms

1
New cards

2667. Create Hello World Function

Write a function createHelloWorld. It should return a new function that always returns "Hello World".

Just return Hello world

2
New cards

2620. Counter

Given an integer n, return a counter function. This counter function initially returns n and then returns 1 more than the previous value every subsequent time it is called (n, n + 1, n + 2, etc).

Just cache the value to be returned. Initially n

3
New cards

2665. Counter

Write a function createCounter. It should accept an initial integer init. It should return an object with three functions.

The three functions are:

  • increment() increases the current value by 1 and then returns it.

  • decrement() reduces the current value by 1 and then returns it.

  • reset() sets the current value to init and then returns it.

Create 2 variables. One to hold the initial value and another one initialzed with the same initial value to play with.

4
New cards

2629. Function Composition

Given an array of functions [f1, f2, f3, ..., fn], return a new function fn that is the function composition of the array of functions.

The function composition of [f(x), g(x), h(x)] is fn(x) = f(g(h(x))).

The function composition of an empty list of functions is the identity function f(x) = x.

You may assume each function in the array accepts one integer as input and returns one integer as output.

  • Start with computed value = initial value from the right.

  • Store the computed value. (computed value initialzed with x)

  • Use the computed value in the subsequent iterations.

5
New cards

2666. Allow One Function Call

Given a function fn, return a new function that is identical to the original function except that it ensures fn is called at most once.

  • The first time the returned function is called, it should return the same result as fn.

  • Every subsequent time it is called, it should return undefined.

Initially is called is false

If is called is false, allow call, else don’t allow