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".
Input: func = () => expect(5).toBe(5)
Output: {"value": true}
Explanation: 5 === 5 so this expression returns true.
Input: func = () => expect(5).toBe(null)
Output: {"error": "Not Equal"}
Explanation: 5 !== null so this expression throws the error "Not Equal".
Input: func = () => expect(5).notToBe(null)
Output: {"value": true}
Explanation: 5 !== null so this expression returns true.
/**
* @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"
*/
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."