JavaScript 回调:第 206 行引用错误:ouput2 未定义
JavaScript Callback: Reference Error on line 206: ouput2 is not defined
我正在尝试创建一个函数,该函数接受两个回调和一个值,该值将 return 一个布尔值,指示是否将值传递给第一个函数,然后将结果输出传递给第二个函数, 产生与反向相同操作相同的输出(将值传递给第二个函数,然后将输出传递给第一个函数)。
我收到以下错误,我认为这可能是由于范围界定所致,但不确定如何解决:Reference Error on line 206: ouput2 is not defined
function commutative(func1, func2, value) {
//check to see if invoking cb1 on value then passing output to cb2 as cb2 => cb1
function func1() {
let output1 = func1(value);
return output1;
}
function func2() {
let output2 = func2(output1);
return output2;
}
function reverseOrder() {
let output3 = func2(value);
let output4 = func1(output3);
}
//return boolean
if (ouput2 === output4) {
return true;
} else {
return false;
}
}
// 测试用例:
const multBy3 = n => n * 3;
const divBy4 = n => n / 4;
const subtract5 = n => n - 5;
console.log(commutative(multBy3, divBy4, 11)); // should log: true
console.log(commutative(multBy3, subtract5, 10)); // should log: false
console.log(commutative(divBy4, subtract5, 48)); // should log: false
您永远不会调用您的内部函数,并且 output1
、output2
等是在这些内部函数的本地定义的,在 commutative
的范围内不可访问。此外,您正在覆盖 func1
和 func2
参数。试试这个:
function commutative(func1, func2, value) {
return func2(func1(value)) === func1(func2(value));
}
// Test cases:
const multBy3 = n => n * 3;
const divBy4 = n => n / 4;
const subtract5 = n => n - 5;
console.log(commutative(multBy3, divBy4, 11)); // should log: true
console.log(commutative(multBy3, subtract5, 10)); // should log: false
console.log(commutative(divBy4, subtract5, 48)); // should log: false
我正在尝试创建一个函数,该函数接受两个回调和一个值,该值将 return 一个布尔值,指示是否将值传递给第一个函数,然后将结果输出传递给第二个函数, 产生与反向相同操作相同的输出(将值传递给第二个函数,然后将输出传递给第一个函数)。
我收到以下错误,我认为这可能是由于范围界定所致,但不确定如何解决:Reference Error on line 206: ouput2 is not defined
function commutative(func1, func2, value) {
//check to see if invoking cb1 on value then passing output to cb2 as cb2 => cb1
function func1() {
let output1 = func1(value);
return output1;
}
function func2() {
let output2 = func2(output1);
return output2;
}
function reverseOrder() {
let output3 = func2(value);
let output4 = func1(output3);
}
//return boolean
if (ouput2 === output4) {
return true;
} else {
return false;
}
}
// 测试用例:
const multBy3 = n => n * 3;
const divBy4 = n => n / 4;
const subtract5 = n => n - 5;
console.log(commutative(multBy3, divBy4, 11)); // should log: true
console.log(commutative(multBy3, subtract5, 10)); // should log: false
console.log(commutative(divBy4, subtract5, 48)); // should log: false
您永远不会调用您的内部函数,并且 output1
、output2
等是在这些内部函数的本地定义的,在 commutative
的范围内不可访问。此外,您正在覆盖 func1
和 func2
参数。试试这个:
function commutative(func1, func2, value) {
return func2(func1(value)) === func1(func2(value));
}
// Test cases:
const multBy3 = n => n * 3;
const divBy4 = n => n / 4;
const subtract5 = n => n - 5;
console.log(commutative(multBy3, divBy4, 11)); // should log: true
console.log(commutative(multBy3, subtract5, 10)); // should log: false
console.log(commutative(divBy4, subtract5, 48)); // should log: false