Skip to content

Commit 625e4dd

Browse files
authored
code added
1 parent 36fe6c6 commit 625e4dd

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

03_toBeOrNotToBe.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Question Link: https://leetcode.com/problems/to-be-or-not-to-be/description/?envType=study-plan-v2&envId=30-days-of-javascript
2+
// Solution Link: https://leetcode.com/problems/to-be-or-not-to-be/solutions/5424106/esay-javascript-solution-time-space-complexity-o-1/
3+
4+
5+
/*
6+
2704. To Be Or Not To Be
7+
8+
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.
9+
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".
10+
notToBe(val) accepts another value and returns true if the two values !== each other. If they are equal, it should throw an error "Equal".
11+
12+
Example 1:
13+
Input: func = () => expect(5).toBe(5)
14+
Output: {"value": true}
15+
Explanation: 5 === 5 so this expression returns true.
16+
Example 2:
17+
Input: func = () => expect(5).toBe(null)
18+
Output: {"error": "Not Equal"}
19+
Explanation: 5 !== null so this expression throw the error "Not Equal".
20+
Example 3:
21+
Input: func = () => expect(5).notToBe(null)
22+
Output: {"value": true}
23+
Explanation: 5 !== null so this expression returns true.
24+
*/
25+
26+
27+
28+
/**
29+
* @param {string} val
30+
* @return {Object}
31+
*/
32+
var expect = function(val) {
33+
return {
34+
toBe: (value) => {
35+
if (value === val) {
36+
return true;
37+
} else {
38+
throw "Not Equal";
39+
}
40+
},
41+
notToBe: (value) => {
42+
if (value !== val) {
43+
return true;
44+
} else {
45+
throw "Equal";
46+
}
47+
}
48+
}
49+
};
50+
51+
/**
52+
* expect(5).toBe(5); // true
53+
* expect(5).notToBe(5); // throws "Equal"
54+
*/

0 commit comments

Comments
 (0)