Creating a custom expect function for JavaScript testing on LeetCode

Day 3 of #30DaysOfJavaScript on LeetCode Today's challenge: 2704 — To Be Or Not To Be The task was to create a custom expect function that helps test code logic — much like how assertion libraries work in testing frameworks. Here’s my solution 👇 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"); } }; }; This challenge was a nice introduction to function objects and error handling in JavaScript — building a mini testing utility was fun! 💡 If you’d like to give it a shot, check it out here: https://lnkd.in/g6WC5mu7 #JavaScript #LeetCode #CodingChallenge #LearningJourney #Closures #30DaysOfCode #ErrorHandling #Testing

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories