简体   繁体   English

函数未返回任何内容

[英]Function is not returning anything

I hava a function, when I run in the console it runs fine, but doesn't run from html file. 我有一个函数,当我在控制台中运行时,它可以正常运行,但不能从html文件运行。

HTML file: HTML档案:

<!DOCTYPE HTML>
<html>
<head>
    <title>func1</title>
    <script type="text/javascript" src='func1.js'></script>
</head>
<body>
<h1>FUNC 1</h1>
</body>
</html>

Js File: Js文件:

function factorial(x){
    var y=1;
    while(x>0){
        y=y*x
        x=x-1;
    }
    return y;
}

factorial(3);

I see nothing in the console. 我在控制台中看不到任何东西。 WHY? 为什么?

You're not seeing anything in the console because you're not actually logging anything to the console. 您没有在控制台中看到任何内容,因为您实际上没有在控制台中记录任何内容。

You need to use console.log(factorial(3)) if you want to see the result of the function. 如果要查看函数结果,则需要使用console.log(factorial(3))

The only reason you see a result when you run the above code directly in the console is because the console outputs the result of each expression it evaluates. 直接在控制台中运行上述代码时看到结果的唯一原因是,因为控制台输出了它评估的每个表达式的结果。 This only applies to things actually inputted into the console, not to all arbitrary JavaScript that runs in your browser. 这仅适用于实际输入到控制台中的内容,不适用于浏览器中运行的所有任意JavaScript。

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

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