To Be Or Not To Be | #2704 | LeetCode Solution

Author: neptune | 03rd-Sep-2023

Problem : To Be Or Not To Be | #2704 | LeetCode

Write a function that helps developers test their code. It should take in any value and return an object with the following two functions.

1. 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".

2. notToBe(val) accepts another value and returns true 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 throws the error "Not Equal".

Example 3:

Input: func = () => expect(5).notToBe(null)

Output: {"value": true}

Explanation: 5 !== null so this expression returns true.


Solution:

        /**

     * @param {string} val

     * @return {Object}

     */

    var expect = function(val) {

        return {

            toBe: function(otherVal) {

                if (val === otherVal){

                    return true

                } else {

                    throw new Error("Not Equal")

                }

            },

            notToBe: function(otherVal) {

                if (val !== otherVal){

                    return true

                } else {

                    throw new Error("Equal")

                }

            }

        }

    };

    /**

     * expect(5).toBe(5); // true

     * expect(5).notToBe(5); // throws "Equal"

     */



Explanation:

1. We start by defining a variable except as a function. This function takes one parameter, val, which is the value you want to perform comparisons on.

2. The toBe function is used for checking if val is equal to otherVal. It takes otherVal as its parameter and performs the comparison.

3. If val is equal to otherVal, it returns an object with a value property set to true, indicating that the values are equal.

4. If val is not equal to otherVal, it throws an error with the message "Not Equal."

5. The notToBe function is used for checking if val is not equal to otherVal. It takes otherVal as its parameter and performs the comparison.

6. If val is not equal to otherVal, it returns an object with a value property set to true, indicating that the values are not equal.

7. If val is equal to otherVal, it throws an error with the message "Equal."




👉 Read More
Generate Fibonacci Sequence - JavaScript | Hackerank
Managing Virtual Environments in React JavaScript Projects
Apply Transform Over Each Element in Array | #2635 | LeetCode Solution
Function Composition | #2629 | LeetCode Solution
Counter | #2620 | LeetCode Solution
Different ways to handle state in React applications
Chunk Array | #2677 | LeetCode Solution
Counter 2 | #2665 | LeetCode Solution
Array Reduce Transformation | #2626 | LeetCode Solution
Add Two Promises | #2723 | LeetCode Solution
Filter Elements from Array | #2634 | LeetCode Solution
Arrow Functions in JavaScript | ES6
From REST to GraphQL: The Future of API Design
Is Object Empty | #2727 | LeetCode | JavaScript Solution
How I Built My Blogging Website Using React, Node.js, and Jamstack Architecture?
How to Perform Unit Testing in React Components with Examples?
Do you know ! How to manage State in Functional & Class Components in React ?
A Guide to Writing Clean, Readable, and Maintainable Code in JavaScript
How to Get Started with Jamstack: A Comprehensive Guide?
Why, What, and When: Understanding Jamstack?
Explore more Blogs...