{ "data": { "question": { "questionId": "2813", "questionFrontendId": "2704", "categoryTitle": "JavaScript", "boundTopicId": 2289200, "title": "To Be Or Not To Be", "titleSlug": "to-be-or-not-to-be", "content": "<p>Write a function <code>expect</code> that helps developers test their code. It should take in any value <code>val</code> and return an object with the following two functions.</p>\n\n<ul>\n\t<li><code>toBe(val)</code> accepts another value and returns <code>true</code> if the two values <code>===</code> each other. If they are not equal, it should throw an error <code>"Not Equal"</code>.</li>\n\t<li><code>notToBe(val)</code> accepts another value and returns <code>true</code> if the two values <code>!==</code> each other. If they are equal, it should throw an error <code>"Equal"</code>.</li>\n</ul>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => expect(5).toBe(5)\n<strong>Output:</strong> {"value": true}\n<strong>Explanation:</strong> 5 === 5 so this expression returns true.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => expect(5).toBe(null)\n<strong>Output:</strong> {"error": "Not Equal"}\n<strong>Explanation:</strong> 5 !== null so this expression throw the error "Not Equal".\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => expect(5).notToBe(null)\n<strong>Output:</strong> {"value": true}\n<strong>Explanation:</strong> 5 !== null so this expression returns true.\n</pre>\n", "translatedTitle": "相等还是不相等", "translatedContent": "<p>请你编写一个名为 <code>expect</code> 的函数,用于帮助开发人员测试他们的代码。它应该接受任何值 <code>val</code> 并返回一个包含以下两个函数的对象。</p>\n\n<ul>\n\t<li><code>toBe(val)</code> 接受另一个值并在两个值相等( <code>===</code> )时返回 <code>true</code> 。如果它们不相等,则应抛出错误 <code>\"Not Equal\"</code> 。</li>\n\t<li><code>notToBe(val)</code> 接受另一个值并在两个值不相等( <code>!==</code> )时返回 <code>true</code> 。如果它们相等,则应抛出错误 <code>\"Equal\"</code> 。</li>\n</ul>\n\n<p> </p>\n\n<p><strong class=\"example\">示例 1:</strong></p>\n\n<pre>\n<b>输入:</b>func = () => expect(5).toBe(5)\n<b>输出:</b>{\"value\": true}\n<b>解释:</b>5 === 5 因此该表达式返回 true。\n</pre>\n\n<p><strong class=\"example\">示例 2:</strong></p>\n\n<pre>\n<b>输入:</b>func = () => expect(5).toBe(null)\n<b>输出:</b>{\"error\": \"Not Equal\"}\n<b>解释:</b>5 !== null 因此抛出错误 \"Not Equal\".\n</pre>\n\n<p><strong class=\"example\">示例 3:</strong></p>\n\n<pre>\n<b>输入:</b>func = () => expect(5).notToBe(null)\n<b>输出:</b>{\"value\": true}\n<b>解释:</b>5 !== null 因此该表达式返回 true.\n</pre>\n", "isPaidOnly": false, "difficulty": "Easy", "likes": 7, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}", "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {string} val\n * @return {Object}\n */\nvar expect = function(val) {\n \n};\n\n/**\n * expect(5).toBe(5); // true\n * expect(5).notToBe(5); // throws \"Equal\"\n */", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "type ToBeOrNotToBe = {\n toBe: (val: any) => boolean;\n notToBe: (val: any) => boolean;\n};\n\nfunction expect(val: any): ToBeOrNotToBe {\n\t\n};\n\n/**\n * expect(5).toBe(5); // true\n * expect(5).notToBe(5); // throws \"Equal\"\n */", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"6.1K\", \"totalSubmission\": \"11.3K\", \"totalAcceptedRaw\": 6102, \"totalSubmissionRaw\": 11310, \"acRate\": \"54.0%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "() => expect(5).toBe(5)", "metaData": "{\n \"name\": \"expect\",\n \"params\": [\n {\n \"name\": \"func\",\n \"type\": \"string\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"manual\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ]\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 5.1.6<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}", "book": null, "isSubscribed": false, "isDailyQuestion": false, "dailyRecordStatus": null, "editorType": "CKEDITOR", "ugcQuestionId": null, "style": "LEETCODE", "exampleTestcases": "() => expect(5).toBe(5)\n() => expect(5).toBe(null)\n() => expect(5).notToBe(null)", "__typename": "QuestionNode" } } }