Skip to content

Commit 017f438

Browse files
author
Sumit Sahoo
committed
Create: 2704-to-be-or-not-to-be.js
1 parent 7f79c9b commit 017f438

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

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

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Creates an expectation object for testing values.
3+
*
4+
* @param {any} val - The value to be tested.
5+
* @returns {Object} An object with two methods:
6+
* - toBe(expected): Returns true if val === expected, otherwise throws an error "Not Equal".
7+
* - notToBe(expected): Returns true if val !== expected, otherwise throws an error "Equal".
8+
*/
9+
const expect = (val) => {
10+
const throwError = (message) => { throw new Error(message); };
11+
return {
12+
toBe: (expected) => val === expected || throwError("Not Equal"),
13+
notToBe: (expected) => val !== expected || throwError("Equal")
14+
};
15+
};
16+
17+
// Example usage:
18+
// expect(5).toBe(5); // returns true
19+
// expect(5).notToBe(3); // returns true
20+
// expect(5).toBe(3); // throws "Not Equal"
21+
// expect(5).notToBe(5); // throws "Equal"

0 commit comments

Comments
 (0)