找到你要的答案

Q:Javascript. Why need to put return keyword in line 2?… 12 is MATIC number and it will be divided by 4

Q:JavaScript。为什么需要将返回关键字放在第2行?…12马蒂奇数和将它除以4

In line 2, why need to put return keyword there ?... 12 is MATIC number and it will be divided by 4 ...

var quarter =function(number) {
    return number/4};  //why need to put return keyword here ?... 12 is MATIC number and it will be divided by 4 ...

if (quarter(12) % 3 === 0 ) {
  console.log("The statement is true");
} else {
  console.log("The statement is false");
}

在第2行中,为什么需要输入返回关键字呢?…12是马蒂奇数就除以4…

var quarter =function(number) {
    return number/4};  //why need to put return keyword here ?... 12 is MATIC number and it will be divided by 4 ...

if (quarter(12) % 3 === 0 ) {
  console.log("The statement is true");
} else {
  console.log("The statement is false");
}
answer1: 回答1:

Otherwise the function wouldn't return an output value when called. For example, you could make several statements inside a function, like:

var quarter =function(number) {
    var foo = "bar",
        person = "john doe";

    return number/4;
}

Calling quarter(12) would still return the same thing. You could also do

var quarter =function(number) {
    var result = number / 4;

    return result;
}

for example

否则,当调用时函数不会返回一个输出值。例如,你可以在函数中做几个语句,比如:

var quarter =function(number) {
    var foo = "bar",
        person = "john doe";

    return number/4;
}

Calling quarter(12) would still return the same thing. You could also do

var quarter =function(number) {
    var result = number / 4;

    return result;
}

例如

answer2: 回答2:

When your if statement call quarter() function and pass 12 to it argument, than quarter() function do division operation on your argument number.

In your scenario quarter() function return 3 which is necessary for the if condition to modulo(%) with 3. So quarter() function is return a number( argument_number/4).

Add below code to print return value of quarter( ) function before if condition.

console.log(quarter(12));

So return value is necessary for the if condition to check return value is modulo(%) by 3 or not.

当你打电话给quarter() if语句的功能和传12这说法,比quarter()功能做除法操作对你的论点的数量。

在这样的情况下quarter()函数返回3,如果条件为模是必要的(%)3。所以quarter()函数返回一个数(argument_number / 4)。

添加下面的代码来打印返回值的季度()功能之前如果条件。

console.log(quarter(12));

所以如果检查返回值的返回值条件模是必要的(%)3或不。

javascript