我是否正确理解了执行顺序? [英] Do I understand the sequence of execution correctly?

查看:50
本文介绍了我是否正确理解了执行顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function factorial(n)

{

for(var f = 1,j = 2; j< = n; j ++)f * = j;

返回f;

}

alert(factorial(5)); // 120



首先:F * = j;默认为1和2;

然后:j< = n; J ++

然后:返回F;

下一个:警报(阶乘(5));



对吗?



谢谢。



我的尝试:



function factorial (n)
{
for (var f = 1, j = 2; j <= n; j++) f *= j;
return f;
}
alert (factorial (5)); // 120

First: F * = j; With default 1 and 2;
Then: j < = n; J++
Then: return F;
Next: Alert (factorial (5));

Right?

Thank you.

What I have tried:

I'm just trying to understand the sequence of code execution.

推荐答案

不,这是一个循环。

它绕过f乘以j直到j大于n。



使用调试器,然后按照它进行操作 - 很明显。
No, it's a loop.
It goes round multiplying f by j until j is larger than n.

Use the debugger, and follow it though - it's pretty obvious.


首先是警报;它将阶乘称为其处理的一部分。



然后f = 1; j = 2;

然后j< = n;

然后f * = j(仅当j <= n)

然后j ++(只有当j <= n)

(循环)时(仅当j <= n)

返回



警报完成。
First is "Alert"; which calls "factorial" as part of it's processing.

Then f=1; j=2;
then j<=n;
then f *= j (only if j<=n)
then j++ (only if j<=n)
(loop) (only if j<=n)
return

Alert finishes.


学习正确编写和缩进代码,它显示其结构,有助于阅读和理解。它还有助于发现结构错误。

Learn to write and indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
function factorial (n)
{
    var f = 1;
    for (j = 2; j <= n; j++)
        f *= j;
    return f;
}



缩进风格 - 维基百科 [ ^ ]



专业程序员的编辑有这个功能和其他功能,如括号匹配和语法高亮。

Notepad++主页 [ ^ ]

ultraedit [ ^ ]


Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

Quote:

我是正确理解执行顺序?

Do I understand the sequence of execution correctly?



请求您的浏览器向您展示代码的真正功能。使用调试器。

-----

你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里显示你的代码正在做什么和你的任务是与它应该做的比较。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到bug,它只是帮助你通过向您展示正在发生的事情。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



JavaScript调试 [< a href =https://www.w3schools.com/js/js_debugging.asptarget =_ blanktitle =New Window> ^ ]

Chrome DevTools  | 网络  |  Google Developers [ ^ ]



这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


Ask your browser to show you what the code really do. Use the debugger.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

JavaScript Debugging[^]
Chrome DevTools  |  Web  |  Google Developers[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于我是否正确理解了执行顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆