我目前正在阅读Eloquent JS图书,但下面的代码我无法理解:
function tableFor(event, journal) {
let table = [0, 0, 0, 0];
for (let i = 0; i < journal.length; i++) {
let entry = journal[i], index = 0;
if (entry.events.includes(event)) index += 1;
if (entry.squirrel) index += 2;
table[index] += 1;
}
return table;
}
console.log(tableFor("pizza", JOURNAL));
// → [76, 9, 4, 1]
您可以在此处查询期刊:https://eloquentjavascript.net/code/#4
这里是一章:https://eloquentjavascript.net/04_data.html-计算相关性
特别是我听不懂这行
let entry = journal[i], index = 0;
我知道我们将日记帐的每个对象都重新分配给条目,但是index = 0是什么呢?以及其他所有索引:
index += 2;
table[index] += 1;
答案 0 :(得分:2)
该函数首先用四个零初始化该table
数组。还请注意,在该循环的每次迭代中index
的可能值为:
这四个值将用作表的索引。循环的每次迭代都会向四个表单元之一添加一个,因此,循环结束后,表将包含这些不同种类条目中每个条目的计数。
哦,在循环的顶部
let entry = journal[i], index = 0;
那只是entry
和index
的声明。
答案 1 :(得分:1)
let entry = journal[i], index = 0
是
的简写let entry = journal[i];
let index = 0;
对于var,const和global声明,其工作方式相似:
var entry = journal[i], index = 0
将翻译为
var entry = journal[i];
var index = 0;
答案 2 :(得分:1)
基本上是位掩码:
如果index
为0,则该条目没有任何要求,并且不包括该事件。
如果index
为1,则条目的事件包括该事件。
如果index
为2,则有一个要求。
如果index
是3(2 + 1),则它有一个问题,并且包含该事件。
现在,该索引用于增加表的一个计数器,表中的第一个值将包含所有没有计数且没有事件的条目的计数,依此类推
答案 3 :(得分:0)
此数组的元素用于跟踪4种情况:
表[0] -当某个事件未发生(例如,没有披萨)并且结果为假(例如,松鼠变换为假)
表[1] -当发生某个事件(例如披萨)但结果为假(例如松鼠变换为假)时
表[2] -当某事件未发生(例如,没有披萨)但结果为真(例如,松鼠变换为真)
表格[3] -当发生某个事件(例如披萨)并且结果为true(例如松鼠变换为true)
让条目=日记[i],索引= 0
journal [i] 基本上是在 for中定义的for循环中,通过日记在第i次迭代中选择对象(journal是对象的数组)。 0; i
索引= 0 也是默认值为0的变量。它用于设置表= [0,0,0,0] 的默认索引(您很快就会看到)
当运行 console.log(tableFor(“ pizza”,JOURNAL))时,for循环位于第一次迭代(i = 0)。它进入journal [0],并设置entry = journal [0]。
它运行 if(entry.events.includes(event)),即是否运行 events (这是日记帐[0]中对象中的一项,现在由条目引用)包含/包含事件,例如比萨。如果是这样,它将使索引增加1(索引+ = 1 )
然后转到下一行 if(entry.squirrel),然后 进行检查,即对象中第二项是否位于journal [0] 发生例如如果松鼠是真的。如果是,它将索引增加2 (索引+ = 2 )。
A。为简单起见,我们假设仅第一个条件为真,即它仅运行 index + = 1 ,因为它在运行(进入.events.includes(event)),它发现了披萨事件,但是当它运行(entry.squirrel)索引+ = 2; 松鼠为假时,因此索引+ = 2将为忽略了。此时,表[索引] =表[1] 由于,索引= 0在 index + = 1 处增加了1其他条件被发现是错误的。然后运行table [1] + = 1,并且该表变为[0,1,0,0]。 (请记住,您可以通过这种方式更改数组的元素(在数组中选择一个索引,然后使用=运算符将其设置为新值)
在检查了第一个条件并且没有发现披萨事件以及松鼠=假的情况下,索引= 0仍然存在,因此table [index] = table [0]。然后,运行表[0] + = 1,该表变为[1、0、0、0]
B。。假设 ONLY (
C。。如果两个 if 条件为true,则在 if(entry.events.includes(event ))index + = 1; ,如果
循环( for(让i = 0; i
希望我能对您有所帮助
答案 4 :(得分:0)
I would like to clarify something to all of you who don't seem to know what math is going on behind the indices.
There's a math going on here. In the book the author uses an array of [76, 9, 4, 1].
In this array, the position of 76 is at index 0, the position of 9 is 1, the position of 4 is 2, and the position of 1 is 3.
When you convert 0 from decimal to binary you get 00 meaning both event did not occur.
When you convert 1 from decimal to binary you get 01 meaning no squirrel but there's pizza
When you convert 2 from decimal to binary you get 10 meaning there is squirrel and no pizza
When you convert 3 from decimal to binary you get 11 meaning there is squirrel and there is pizza.
76 gets position 0 corresponding to 00 meaning there was no squirrel and no pizza at the same time 76 times
9 gets position 1 corresponding to 01 meaning there was no squirrel and there was pizza at the same time 9 times
4 gets position 2 corresponding to 10 meaning there was squirrel and no pizza at the same time 4 times
1 gets position 3 corresponding to 11 meaning there was squirrel and there was pizza at the same time just 1 time
Now when you write your code, [0,0,0,0] follows the same logic.