LeetCode Problem 2704 To Be Or Not To Be — LeetCode: 30 Days of JavaScript
Solving Leetcode 30 Days of JavaScript study plan problems
Problem:
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.
toBe(val)
accepts another value and returnstrue
if the two values===
each other. If they are not equal, it should throw an error"Not Equal"
.notToBe(val)
accepts another value and returnstrue
if the two values!==
each other. If they are equal, it should throw an error"Equal"
.
Example 1:
Input: func = () => expect(5).toBe(5)
Output: {"value": true}
Explanation: 5 === 5 so this expression returns true.
Example 2:
Input: func = () => expect(5).toBe(null)
Output: {"error": "Not Equal"}
Explanation: 5 !== null so this expression throw the error "Not Equal".
Example 3:
Input: func = () => expect(5).notToBe(null)
Output: {"value": true}
Explanation: 5 !== null so this expression returns true.
Solution JavaScript:
/**
* @param {string} val
* @return {Object}
*/
var expect = function(val) {
function toBe(val2) {
if (val === val2) {
return true
} else {
throw new Error('Not Equal')
}
}
function notToBe(val2){
if (val !== val2) {
return true
} else {
throw new Error('Equal')
}
}
return {
toBe,
notToBe
}
};
Inside the expect
function, two internal functions are defined:
function toBe(val2) { ... }
: This function checks if the providedval
is strictly equal toval2
. If they are equal, it returnstrue
. Otherwise, it throws an error with the message 'Not Equal'.function notToBe(val2) { ... }
: This function checks if the providedval
is not strictly equal toval2
. If they are not equal, it returnstrue
. Otherwise, it throws an error with the message 'Equal'.
The expect
function returns an object with two properties, toBe
and notToBe
, which hold references to the internal functions toBe
and notToBe
respectively.
This code essentially provides a simple way to perform checks and assertions in JavaScript code. By using the expect
function, along with its toBe
and notToBe
methods, you can easily test if values are as expected and throw errors if they are not, allowing for a simple testing framework.
SolutionTypeScript:
type ToBeOrNotToBe = {
toBe: (val: any) => boolean;
notToBe: (val: any) => boolean;
};
function expect(val: any): ToBeOrNotToBe {
return {
toBe: (otherVal: any) => {
if (val === otherVal) {
return true;
} else {
throw new Error("Not Equal");
}
},
notToBe: (otherVal: any) => {
if(val !== otherVal) {
return true;
} else {
throw new Error("Equal");
}
}
};
};
Inside the expect
function:
return { ... }
: This line returns an object with two properties,toBe
andnotToBe
, where each property is a function.toBe: (otherVal: any) => { ... }
: This function checks if theval
is strictly equal to theotherVal
parameter. If they are equal, it returnstrue
; otherwise, it throws an error with the message "Not Equal".notToBe: (otherVal: any) => { ... }
: This function checks if theval
is not strictly equal to theotherVal
parameter. If they are not equal, it returnstrue
; otherwise, it throws an error with the message "Equal".