EASY
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"
.寫一個函數“expect”來幫助開發人員測試他們的代碼。 它應該接受任何參數“val”並返回一個具有以下兩個函數的物件:
toBe(val)
接受另一個參數,如果兩個值彼此 ===
則返回 true
。 如果它們不相等,則應拋出錯誤“不等於”。notToBe(val)
接受另一個參數,如果兩個值 !==
彼此相等,則返回 true
。 如果它們相等,它應該拋出錯誤“Equal”。expect
函式 函式會返回 {toBe,notToBe}'兩個函式const expect = (val1)=>{
toBe = (val2) => {
return ;
};
notToBe = (val2) => {
return ;
};
return {toBe,notToBe}
}
const expect = (val1)=>{
toBe = (val2) => {
return val1 === val2? true : throw new Error ("Not Equal") ;
};
notToBe = (val2) => {
return val1 !== val2? true : throw new Error ("Equal") ;
};
return {toBe,notToBe}
}
const expect = (val1)=>{
throwErr= (str)=>{
throw new Error(str);
}
toBe = (val2) => {
return val1 === val2? true : throwErr("Not Equal") ;
};
notToBe = (val2) => {
return val1 !== val2? true : throwErr("Equal") ;
};
return {toBe,notToBe}
}
const expect = (val1) => {
throwErr= (str) => {throw new Error(str)};
return {
toBe: (val2) => val2 === val1 || throwErr("Not Equal"),
notToBe: (val2) => val2 !== val1 || throwErr("Equal"),
};
};
const A = expect(5);
console.log(A.toBe(5));
// console.log(A.toBe(null));
console.log(A.notToBe(null));