我正在编写带参数的函数表达式,它看起来像这个
var n1 = 10;
var n2 = 15;
var num = function (n1, n2) {return (n1 > n2) ? n1 : n2;}
console.log("The larger number of " + n1 + " and " + n2 + " is " + num);
但是在控制台中我收到的消息是
The larger number of 10 and 15 is function (n1, n2) {return (n1 > n2) ? n1 : n2;}
我无法弄清楚我在这里做错了什么......
答案 0 :(得分:1)
将console.log("The larger number of " + n1 + " and " + n2 + " is " + num);
更改为console.log("The larger number of " + n1 + " and " + n2 + " is " + num(n1,n2));
var n1 = 10;
var n2 = 15;
var num = function (n1, n2) {return (n1 > n2) ? n1 : n2;}
console.log("The larger number of " + n1 + " and " + n2 + " is " + num(n1,n2));
答案 1 :(得分:1)
使用num(n1, n2)
代替num
,因为您必须调用该函数。提示:尝试使用AirBnb Javascript style guide:
console.log(`The larger number of ${n1} and ${n2} is ${num(n1,n2)}`);
答案 2 :(得分:1)
输入参数并执行函数:
console.log("The larger number of " + n1 + " and " + n2 + " is " + num(n1,n2));