Skip to content

Commit 1065a0c

Browse files
authored
feat: add solutions to lc problem: No.2704 (doocs#1452)
No.2704.To Be Or Not To Be
1 parent 62f7cd4 commit 1065a0c

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed

solution/2700-2799/2704.To Be Or Not To Be/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,62 @@
5050
<!-- 这里可写当前语言的特殊实现逻辑 -->
5151

5252
```ts
53+
type ToBeOrNotToBe = {
54+
toBe: (val: any) => boolean;
55+
notToBe: (val: any) => boolean;
56+
};
57+
58+
function expect(val: any): ToBeOrNotToBe {
59+
return {
60+
toBe: (toBeVal: any) => {
61+
if (val !== toBeVal) {
62+
throw new Error('Not Equal');
63+
}
64+
return true;
65+
},
66+
notToBe: (notToBeVal: any) => {
67+
if (val === notToBeVal) {
68+
throw new Error('Equal');
69+
}
70+
return true;
71+
},
72+
};
73+
}
74+
75+
/**
76+
* expect(5).toBe(5); // true
77+
* expect(5).notToBe(5); // throws "Equal"
78+
*/
79+
```
5380

81+
### **JavaScript**
82+
83+
```js
84+
/**
85+
* @param {string} val
86+
* @return {Object}
87+
*/
88+
var expect = function (val) {
89+
return {
90+
toBe: function (expected) {
91+
if (val !== expected) {
92+
throw new Error('Not Equal');
93+
}
94+
return true;
95+
},
96+
notToBe: function (expected) {
97+
if (val === expected) {
98+
throw new Error('Equal');
99+
}
100+
return true;
101+
},
102+
};
103+
};
104+
105+
/**
106+
* expect(5).toBe(5); // true
107+
* expect(5).notToBe(5); // throws "Equal"
108+
*/
54109
```
55110

56111
<!-- tabs:end -->

solution/2700-2799/2704.To Be Or Not To Be/README_EN.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,62 @@
4343
### **TypeScript**
4444

4545
```ts
46+
type ToBeOrNotToBe = {
47+
toBe: (val: any) => boolean;
48+
notToBe: (val: any) => boolean;
49+
};
50+
51+
function expect(val: any): ToBeOrNotToBe {
52+
return {
53+
toBe: (toBeVal: any) => {
54+
if (val !== toBeVal) {
55+
throw new Error('Not Equal');
56+
}
57+
return true;
58+
},
59+
notToBe: (notToBeVal: any) => {
60+
if (val === notToBeVal) {
61+
throw new Error('Equal');
62+
}
63+
return true;
64+
},
65+
};
66+
}
67+
68+
/**
69+
* expect(5).toBe(5); // true
70+
* expect(5).notToBe(5); // throws "Equal"
71+
*/
72+
```
4673

74+
### **JavaScript**
75+
76+
```js
77+
/**
78+
* @param {string} val
79+
* @return {Object}
80+
*/
81+
var expect = function (val) {
82+
return {
83+
toBe: function (expected) {
84+
if (val !== expected) {
85+
throw new Error('Not Equal');
86+
}
87+
return true;
88+
},
89+
notToBe: function (expected) {
90+
if (val === expected) {
91+
throw new Error('Equal');
92+
}
93+
return true;
94+
},
95+
};
96+
};
97+
98+
/**
99+
* expect(5).toBe(5); // true
100+
* expect(5).notToBe(5); // throws "Equal"
101+
*/
47102
```
48103

49104
<!-- tabs:end -->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} val
3+
* @return {Object}
4+
*/
5+
var expect = function (val) {
6+
return {
7+
toBe: function (expected) {
8+
if (val !== expected) {
9+
throw new Error('Not Equal');
10+
}
11+
return true;
12+
},
13+
notToBe: function (expected) {
14+
if (val === expected) {
15+
throw new Error('Equal');
16+
}
17+
return true;
18+
},
19+
};
20+
};
21+
22+
/**
23+
* expect(5).toBe(5); // true
24+
* expect(5).notToBe(5); // throws "Equal"
25+
*/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
type ToBeOrNotToBe = {
2+
toBe: (val: any) => boolean;
3+
notToBe: (val: any) => boolean;
4+
};
5+
6+
function expect(val: any): ToBeOrNotToBe {
7+
return {
8+
toBe: (toBeVal: any) => {
9+
if (val !== toBeVal) {
10+
throw new Error('Not Equal');
11+
}
12+
return true;
13+
},
14+
notToBe: (notToBeVal: any) => {
15+
if (val === notToBeVal) {
16+
throw new Error('Equal');
17+
}
18+
return true;
19+
},
20+
};
21+
}
22+
23+
/**
24+
* expect(5).toBe(5); // true
25+
* expect(5).notToBe(5); // throws "Equal"
26+
*/

0 commit comments

Comments
 (0)