{ "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": "
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 returns true
if the two values ===
each other. If they are not equal, it should throw an error "Not Equal"
.notToBe(val)
accepts another value and returns true
if the two values !==
each other. If they are equal, it should throw an error "Equal"
.\n
Example 1:
\n\n\nInput: func = () => expect(5).toBe(5)\nOutput: {"value": true}\nExplanation: 5 === 5 so this expression returns true.\n\n\n
Example 2:
\n\n\nInput: func = () => expect(5).toBe(null)\nOutput: {"error": "Not Equal"}\nExplanation: 5 !== null so this expression throw the error "Not Equal".\n\n\n
Example 3:
\n\n\nInput: func = () => expect(5).notToBe(null)\nOutput: {"value": true}\nExplanation: 5 !== null so this expression returns true.\n\n", "translatedTitle": "相等还是不相等", "translatedContent": "
请你编写一个名为 expect
的函数,用于帮助开发人员测试他们的代码。它应该接受任何值 val
并返回一个包含以下两个函数的对象。
toBe(val)
接受另一个值并在两个值相等( ===
)时返回 true
。如果它们不相等,则应抛出错误 \"Not Equal\"
。notToBe(val)
接受另一个值并在两个值不相等( !==
)时返回 true
。如果它们相等,则应抛出错误 \"Equal\"
。\n\n
示例 1:
\n\n\n输入:func = () => expect(5).toBe(5)\n输出:{\"value\": true}\n解释:5 === 5 因此该表达式返回 true。\n\n\n
示例 2:
\n\n\n输入:func = () => expect(5).toBe(null)\n输出:{\"error\": \"Not Equal\"}\n解释:5 !== null 因此抛出错误 \"Not Equal\".\n\n\n
示例 3:
\n\n\n输入:func = () => expect(5).notToBe(null)\n输出:{\"value\": true}\n解释:5 !== null 因此该表达式返回 true.\n\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\": 6101, \"totalSubmissionRaw\": 11309, \"acRate\": \"53.9%\"}", "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\",\"
\\u7248\\u672c\\uff1a \\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\" TypeScript 5.1.6<\\/p>\\r\\n\\r\\n Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c 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"
}
}
}Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n
--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n