Skip to content

Commit 33fbc71

Browse files
authored
Create 2704-to-be-or-not-to-be.ts
1 parent 5ef4f67 commit 33fbc71

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

code/easy/2704-to-be-or-not-to-be.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Title: To Be Or Not To Be
2+
// Description:
3+
// Write a function expect that helps developers test their code. It should take in any value val and return an object with the following two functions.
4+
// toBe(val) accepts another value and returns true if the two values === each other. If they are not equal, it should throw an error "Not Equal".
5+
// notToBe(val) accepts another value and returns true if the two values !== each other. If they are equal, it should throw an error "Equal"
6+
// Link: https://leetcode.com/problems/to-be-or-not-to-be/
7+
8+
type ToBeOrNotToBe = {
9+
toBe: (val: any) => boolean;
10+
notToBe: (val: any) => boolean;
11+
};
12+
13+
function raise(err: unknown): never {
14+
throw err;
15+
}
16+
17+
function expect(value: any): ToBeOrNotToBe {
18+
return {
19+
toBe: (val) => value === val || raise("Not Equal"),
20+
notToBe: (val) => value !== val || raise("Equal"),
21+
};
22+
}
23+
24+
/**
25+
* expect(5).toBe(5); // true
26+
* expect(5).notToBe(5); // throws "Equal"
27+
*/

0 commit comments

Comments
 (0)