我试图在这里创建一个assertTesting函数,它将实际值和期望值与严格相等(不是类型转换)进行比较。
作为一个例子,这是一个成功案例:
function multiplyByTwo(n) {
return n * 2;
}
var output = multiplyByTwo(2); // returns 4
assertTesting(output, 4, 'it doubles 2 to 4');
// console output:
// passed
这是一个失败的人:
function multiplyByTwo(n) {
return (n * 2) + 1; // an incorrect implementation
}
var output = multiplyByTwo(2); // returns 5
assertTesting(output, 4, 'it doubles 2 to 4');
// console output:
// FAILED [it doubles 2 to 4] Expected "4", but got "5"
到目前为止,这是我的代码:
var output;
function multiplyByTwo(n) {
output = n * 2;
return output;
}
function assertTesting (actual, expected, testName) {
if(actual === ouput && expected === testName){
return "SUCCESS" + [testname] + "Expected" + expected + "," "but got" + actual;
}
}
任何想法如何才能获得我需要的确切输出?请帮忙。
答案 0 :(得分:1)
应该是这样的,我想:
function assertEqual(actual, expected, testName) {
if(actual !== expected){
console.error( "FAIL [" + testname + "] Expected \"" + expected + ", \" but got \"" + actual + "\"");
} else {
console.info( "SUCCESS [" + testname + "]");
}
}