Skip to content

Commit 0386cff

Browse files
committed
Add solution #2704
1 parent 5680a34 commit 0386cff

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@
407407
2677|[Chunk Array](./2677-chunk-array.js)|Easy|
408408
2695|[Array Wrapper](./2695-array-wrapper.js)|Easy|
409409
2703|[Return Length of Arguments Passed](./2703-return-length-of-arguments-passed.js)|Easy|
410+
2704|[To Be Or Not To Be](./2704-to-be-or-not-to-be.js)|Easy|
410411
2705|[Compact Object](./2705-compact-object.js)|Medium|
411412
2715|[Timeout Cancellation](./2715-timeout-cancellation.js)|Easy|
412413
2721|[Execute Asynchronous Functions in Parallel](./2721-execute-asynchronous-functions-in-parallel.js)|Medium|

solutions/2704-to-be-or-not-to-be.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 2704. To Be Or Not To Be
3+
* https://leetcode.com/problems/to-be-or-not-to-be/
4+
* Difficulty: Easy
5+
*
6+
* Write a function expect that helps developers test their code. It should take in any
7+
* value val and return an object with the following two functions.
8+
*
9+
* - toBe(val) accepts another value and returns true if the two values === each other.
10+
* If they are not equal, it should throw an error "Not Equal".
11+
* - notToBe(val) accepts another value and returns true if the two values !== each
12+
* other. If they are equal, it should throw an error "Equal".
13+
*/
14+
15+
/**
16+
* @param {string} val
17+
* @return {Object}
18+
*/
19+
var expect = function(val) {
20+
const compareHelper = (error, fn) => {
21+
if (fn()) throw new Error(error);
22+
return true;
23+
};
24+
return {
25+
toBe: input => compareHelper('Not Equal', () => input !== val),
26+
notToBe: input => compareHelper('Equal', () => input === val),
27+
};
28+
};

0 commit comments

Comments
 (0)