雄辩的JavaScript第4章计算相关性



我目前正在通过书籍雄辩的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;

我知道我们将日记帐的每个对象都重新分配到输入,但是索引= 0有什么?还有其他索引:

index += 2;
table[index] += 1;

函数首先初始化了带有四个零的 table数组。还要注意,该循环的每个迭代中index的可能值是:

  • 0,如果条目不包括"事件",并且没有"松鼠"标志集;
  • 1,如果条目确实包括"事件",但没有松鼠;
  • 2,如果条目不包括"事件",而是松鼠;
  • 3,如果条目都包括"事件",并且是松鼠

这四个值将充当表中的索引。循环的每次迭代都会添加四个表单元格之一,因此,循环完成后,表包含每种不同类型的条目的计数。

哦,在循环的顶部

     let entry = journal[i], index = 0;

这只是entryindex的声明。

let entry = journal[i], index = 0

的速记
let entry = journal[i];
let index = 0;

它适用于VAR,CONS和全局声明:

var entry = journal[i], index = 0

将转化为

var entry = journal[i];
var index = 0;

基本上是一个bitmask:

如果 index为0,则该条目不包括鼠标,并且不包括事件。

如果index是1,则该条目的事件包括事件。

如果 index是2

如果 index为3(2 1(,则包括一只松鼠,并包括事件。

现在使用索引来增加表的计数器之一,表中的第一个值将包含所有条目的计数,没有任何squil鼠,没有事件,等等

  1. 表= [0,0,0,0]

此数组的元素用于跟踪4个情况:

表[0] - 当未发生某个事件(例如,没有披萨(时,结果是错误的(例如,松鼠转换为false(

表[1] - 发生某个事件时(例如披萨(,但结果是错误的(例如,松鼠转换为false(

表[2] - 没有发生某个事件(例如,没有披萨(,但结果是正确的(例如,松鼠转换为true(

表[3] - 发生某个事件时(例如披萨(,结果为真(例如,松鼠转换为真(

  1. LET ENTRY =日记[I],index = 0

    日记[i] 基本上在i-th Iteeration通过日记中选择对象(日记本是对象的数组(,在> for(让i = i =(中定义的for-loop中。0; i&lt; journal.length; i (并将其存储在变量条目中。

index = 0 也是一个变量,默认值为0。它用于设置 table = [0、0、0、0、0] 的默认索引(您很快就会看到(

  1. 当运行 cons> Console.log(tablefor(" pizza",journal((正在运行时,for-loop在第一次迭代中(i = 0(。它用于日记[0],并设置entry =日记[0]。

    • 它运行 if(entry.events.includes(event((即(这是日记[0]的对象中的一个项目 - 条目引用的(包含/包括事件,例如比萨。如果是这样,它将索引增加1( index = 1 (

    • 然后转到下一行 if(entry.squirrel(和检查是否在日记[0]上的对象中的第二个项目是否是发生,例如如果松鼠是真的。如果是这样,它将索引增加2( index = 2 (。

a。为了简单起见,假设只有第一个条件为true,即仅运行 index = 1 ,因为当它ran ran (条目(。被忽略。在这一点已经发现其他条件是错误的。然后运行表[1] = 1,表变为[0、1、0、0]。(请记住,您可以以这种方式更改数组的元素(在数组中选择一个索引,并使用= operator设置为新值((

在检查第一个条件并且没有发现披萨事件以及松鼠= false = false,index = 0仍然存在,因此表[index] =表[0]。然后运行表[0] = 1,表变为[1,0,0,0]

b。假设 if(entry.squirrel(index = 2; 是TRUE,而先前的行则(entry.events.includes(event((索引 = 1; 如此之所以使索引= 0保留,索引从0增加到2,因为 index = 2 。此点表[index] =表[2]。当表[2] = 1运行并且表变为[0、0、1、0]时

c。在两个情况下,如果条件为真,则index = 0在上增加1个,如果(entry.events.includes.includes(事件(((index = 1; ,然后在上以2为2 if(entry.squirrel(index = 2; 。即索引= 0 1 2 = 3。此时,索引= 3和表[index] =表[3]。因此,表[3] = 1更新表到表[0、0、0、1]

loop( for(让i = 0; i&lt; journal.length; i ((从日记[0] toll tolle 日记[长度-1] ,更新表= [0,0,0,0] 从索引到索引,每次在每个表索引上增加一个项目,这是由于表,[索引] = 1 ,在检查条件时,直到检查了日记帐数组中的最后一个对象并完成了更新。

我希望我对您有所帮助

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.

最新更新