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

时间:2019-05-06 14:32:48

标签: javascript

我目前正在阅读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;

5 个答案:

答案 0 :(得分:2)

该函数首先用四个零初始化该table数组。还请注意,在该循环的每次迭代中index的可能值为:

  • 0,如果该条目不包含“事件”,并且未设置“松鼠”标志;
  • 1,如果该条目确实包含“事件”但没有松鼠;
  • 2,如果条目不包含“事件”而是松鼠;
  • 3,如果条目既包含“事件”又是松鼠

这四个值将用作表的索引。循环的每次迭代都会向四个表单元之一添加一个,因此,循环结束后,表将包含这些不同种类条目中每个条目的计数。

哦,在循环的顶部

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

那只是entryindex的声明。

答案 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)

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

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

表[0] -当某个事件未发生(例如,没有披萨)并且结果为假(例如,松鼠变换为假)

表[1] -当发生某个事件(例如披萨)但结果为假(例如松鼠变换为假)时

表[2] -当某事件未发生(例如,没有披萨)但结果为真(例如,松鼠变换为真)

表格[3] -当发生某个事件(例如披萨)并且结果为true(例如松鼠变换为true)

  1. 让条目=日记[i],索引= 0

    journal [i] 基本上是在 for中定义的for循环中,通过日记在第i次迭代中选择对象(journal是对象的数组)。 0; i 并将其存储在变量 entry 中。

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

  1. 当运行 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 .squirrel)索引+ = 2; 为true,并且上一行 if (entry.events.includes(event))index + = 1; 使得index = 0保留,因为 index + = 2 ,索引从0增加到2。此时,table [index] = table [2]。当运行table [2] + = 1并且表变为[0,0,1,0]时,因为表转到索引2并将其设置为1。

C。。如果两个 if 条件为true,则在 if(entry.events.includes(event ))index + = 1; ,如果,则再加2。即index = 0 +1 +2 =3。这时,index = 3且table [index] = table [3]。因此,table [3] + = 1将表更新为table [0,0,0,1]

循环( for(让i = 0; i )从 journal [0] journal [length -1] ,将 table = [0,0,0,0] 从索引更新到索引,由于 table [],一次将每个表的索引项增加一个。 index] + = 1 ,直到检查条件为止,直到检查日志数组中的最后一个对象并且表完成更新为止。

希望我能对您有所帮助

答案 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.