File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change
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
+ */
You can’t perform that action at this time.
0 commit comments