Javascript嵌套for循环和array.reduce(Javascript nested for loop and array.reduce)

编程入门 行业动态 更新时间:2024-10-26 23:25:06
Javascript嵌套for循环和array.reduce(Javascript nested for loop and array.reduce)

我是编程的新手和这个网站的新手,我有一个关于嵌套for循环的问题。 这是代码:

function eddardStarkSecret() { var ans =[]; var map = [ [111, 1, 6], [110, 2, 5, 22], [115, 10], [83, 4], [119, 7], [105, 9], [97, 12, 15, 18], [84, 14], [114, 16, 19], [103, 17], [121, 20], [101, 21], [32, 3, 8, 11, 13], [74,0] ]; for (var i = 0; i < map.length; i++) { for (var j = 1; j < map[i].length; j++) ans[map[i][j]] = map[i][0]; } return ans.reduce(function (prev, curr) { return prev + String.fromCharCode(curr); },""); }

代码的输出是这样的:“Jon Snow是一个Targaryen”

现在,我的问题是,它是怎么发生的? 我对array.reduce()如何工作有一点点想法。 我真正感到困惑的部分是嵌套for循环是如何工作的(比如它如何产生32个charcode(空格键)以及它是如何使数组长度为23并产生所需输出所需的单词)。

任何帮助将非常感激。 很多,非常感谢提前。

I am a newbie in programming and new to this site and I have a question about nested for loops. This is the code:

function eddardStarkSecret() { var ans =[]; var map = [ [111, 1, 6], [110, 2, 5, 22], [115, 10], [83, 4], [119, 7], [105, 9], [97, 12, 15, 18], [84, 14], [114, 16, 19], [103, 17], [121, 20], [101, 21], [32, 3, 8, 11, 13], [74,0] ]; for (var i = 0; i < map.length; i++) { for (var j = 1; j < map[i].length; j++) ans[map[i][j]] = map[i][0]; } return ans.reduce(function (prev, curr) { return prev + String.fromCharCode(curr); },""); }

The ouput of the code is this: "Jon Snow is a Targaryen"

Now, my question is, how did it happened? I have a little idea on how array.reduce() part works. The part where i am really confused at is how the nested for loop worked(like how did it produce the 32 charcode(space bar) and how did it made the array length to 23 and produce the words needed for the desired output).

Any help would be very much be appreciated. Many, many thanks in advance.

最满意答案

理解所发生的事情的最好方法就是在调试器中运行这些代码,逐步浏览它。 您的Web浏览器内置了一个全功能的调试器,您可以使用该调试器,或者使用任何JavaScript IDE。

但简单地说:

在map数组的子数组中,第一个条目是字符代码,所有后续条目都是该字符应该使用的地方。 因此,例如, [111, 1, 6]告诉我们字符代码111( "o" )应该在结果的位置1和6处。

我真的很困惑的部分是嵌套for循环是如何工作的(比如它是如何产生32个charcode(空格键)

32是map数组倒数第二项中的第一个值,它是[32, 3, 8, 11, 13] ,因此它被放置在位置3,8,11和13。

它是如何使数组长度达到23并产生所需输出所需的单词的。

通过循环遍历子数组,跳过第一个条目(通过从1开始而不是1),并使用所有其他索引作为索引到ans ,它将ans中第一个条目(字符代码)的值存储在其中:

// The char code vvvvvvvvv ans[map[i][j]] = map[i][0]; // ^^^^^^^^^--- where to put it

然后在最后它通过使用String.fromCharCode将每个条目转换为其字符并将其附加到与reduce使用的累加器来循环。

The best way to understand what's happening is to run this code in a debugger, walking through it step by step. Your web browser has a fully-featured debugger built into it which you can use for this, or any JavaScript IDE does.

But briefly:

In the map array's subarrays, the first entry is the character code and all subsequent entries are where that character should be used. So for instance, [111, 1, 6] tells us character code 111 ("o") should be at positions 1 and 6 in the result.

The part where i am really confused at is how the nested for loop worked(like how did it produce the 32 charcode(space bar)

The 32 is the first value in the second-to-last entry in the map array, which is [32, 3, 8, 11, 13], and so it's put at positions 3, 8, 11, and 13.

and how did it made the array length to 23 and produce the words needed for the desired output).

By looping through the subarrays, skipping the first entry (by starting j at 1 instead of ), and using all the others as indexes into ans, where it stores the value of the first entry (the character code) in ans:

// The char code vvvvvvvvv ans[map[i][j]] = map[i][0]; // ^^^^^^^^^--- where to put it

Then at the end it just loops through ans using String.fromCharCode to convert each entry to its character and append it to the accumulator used with reduce.

更多推荐

本文发布于:2023-08-01 09:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1356721.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   array   Javascript   loop   nested

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!