简体   繁体   English

为什么此函数返回false?

[英]Why does this function ever return false?

assuming that an input was not equal to 1 or 2 (eg an input of 15), it would go through the loop, return false, but wouldn't that return value be overridden by the 'return true' underneath it that's outside of the for loop? 假设输入不等于1或2(例如输入15),它将经过循环,返回false,但是返回值不会被位于其外部的'return true'覆盖。循环?

help to understand this would be much appreciated. 帮助理解这一点将不胜感激。

function checkIfPrime(numb) {
    if (numb === 1) {
        return false;
    } else if (numb === 2) {
        return true;
    } else {
        for (let x = 2; x < numb; x++) {
            if (numb % x === 0) {
                return false;
            }
        }
        return true;
    }
}

console.log(checkIfPrime(2));
console.log(checkIfPrime(15));
console.log(checkIfPrime(17));
console.log(checkIfPrime(19));

wouldn't that return value be overridden by the 'return true' underneath? 该返回值不会被下面的“ return true”覆盖吗?

Well, no. 好吧,不。 When you return false the entire function execution stops and the return value is returned. 当您return false ,整个函数将停止执行,并return值。

You give the example of numb = 15 . 您以numb = 15为例。 Obviously 15 is not a prime number. 显然15不是素数。 The function will return false once x =3 within the for-loop . 一旦在for-loop x =3该函数将return false At this point the function execution will terminate completely and return the value false . 此时,函数执行将完全终止,并返回值false It will not progress to the return true statement at all. 它根本不会前进到return true语句。

For a prime number example, say numb= 17 , the for loop will execute and the if-statement will never be true. 对于素数示例,例如numb= 17 ,将执行for循环,并且if-statement永远不会为真。 This means the function execution will progress and the return true statement will be executed, thus making the function return true . 这意味着函数将继续执行,并且将执行return true语句,从而使函数返回true

Check out this W3 Schools documentation for furter explanation of the return statement. 请查阅W3 Schools文档 ,以获取对return声明的进一步解释。

As TJ Crowder suggested in the comments, using your IDE's debugger would be useful for you to follow the execution. 正如TJ Crowder在评论中所建议的那样,使用IDE的调试器对您跟踪执行很有用。

when return statement is executed, statements after return are not executed. 执行return语句时,不执行return之后的语句。 Program leaves the function and go to the statement calling that function. 程序离开函数,然后转到调用该函数的语句。

return is different from a break; 回报不同于休息; it doesn't just break out of the loop, but return the value for the whole function. 它不仅会跳出循环,还会返回整个函数的值。 Hope this helps. 希望这可以帮助。

Gav. GAV。

return is a terminator so the current code block exits upon meeting the statement return是终止符,因此当前代码块在遇到该语句后退出

the behaviour you are describing would be created by this code 您描述的行为将由此代码创建

function incorrectCheckIfPrime(numb) {
    var returnValue;
    if (numb === 1) {
        returnValue = false;
    } else if (numb === 2) {
        returnValue = true;
    } else {
        for (let x = 2; x < numb; x++) {
            if (numb % x === 0) {
                returnValue = false;
            }
        }
        returnValue =true;
    }
    return returnValue;
}

console.log(checkIfPrime(2));
console.log(checkIfPrime(15));
console.log(checkIfPrime(17));
console.log(checkIfPrime(19));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM