【问题标题】:Eloquent javascript chapter 4 Computing correlationEloquent javascript 第4章计算相关性
【发布时间】:2019-05-06 14:32:48
【问题描述】:

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

您可以在这里查看 JOURNAL:https://eloquentjavascript.net/code/#4

这里的章节:https://eloquentjavascript.net/04_data.html - 计算相关性

尤其看不懂这一行

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

我知道我们将日志的每个对象重新分配给条目,但是 index=0 有什么作用?以及其他所有索引:

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

【问题讨论】:

    标签: javascript


    【解决方案1】:

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

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

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

    哦,在循环的顶部

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

    这只是entryindex 的声明。

    【讨论】:

      【解决方案2】:
      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;
      

      【讨论】:

      • 天哪!我忘了我们可以在一行中分配变量。 :) 谢谢人!
      • 您不会经常看到它,因为根据我的经验,它通常被认为是“不好的做法”。尤其是 eslint:Expected variable declaration to be on a new line.eslint(one-var-declaration-per-line)
      【解决方案3】:

      它基本上是一个位掩码:

      如果index 为0,则该条目没有松鼠并且不包含该事件。

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

      如果index 是 2,它有一个松鼠。

      如果index 为 3 (2 + 1),则它有一个松鼠并且包含该事件。

      现在索引用于增加表中的一个计数器,表中的第一个值将包含所有条目的计数,没有松鼠和没有事件等等

      【讨论】:

        【解决方案4】:
        1. table = [0, 0, 0, 0]

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

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

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

        table[2] - 当某个事件没有发生(例如没有披萨)但结果为真(例如松鼠变形为真)时

        table[3] - 当某个事件发生(例如披萨)并且结果为真(例如松鼠变形为真)时

        1. 让 entry = journal[i], index = 0

          journal[i] 基本上在通过journal 的第i 次迭代中选择对象(journal 是一个对象数组),在for (let i = 0; i 并将其存储在变量 entry 中。

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

        1. console.log(tableFor("pizza", JOURNAL)) 运行时,for 循环在第一次迭代 (i=0) 上。它转到日志 [0],并设置条目 = 日志 [0]。

          • 它运行 if (entry.events.includes(event)) 即如果 events (它是 journal[0] 的对象中的一个项目 - 现在由条目引用)包含/包含事件,例如比萨。如果是,它会将索引增加 1 (index += 1)

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

        A. 为简单起见,我们假设只有第一个条件为真,即它只运行 index += 1 因为当它运行 (entry .events.includes(event)),它找到了披萨事件,但是当它运行时 (entry.squirrel) index += 2; squirrel 为假,因此 index +=2 将是忽略。此时,table[index] = table[1] BECAUSE index = 0 在 index += 1 处增加了 1其他条件被发现是错误的。然后运行 ​​table[1] += 1 并且 table 变为 [0, 1, 0, 0]。 (请记住,您可以通过这种方式更改数组的元素(选择数组中的索引并使用 = 运算符将其设置为新值))

        如果检查了第一个条件并且没有找到比萨饼事件以及 squirrel=false,则 index = 0 仍然存在,因此 table[index] = table[0]。然后运行table[0] += 1,table变为[1, 0, 0, 0]

        B. 假设 ONLY if (entry.squirrel) index += 2; 为真,而上一行 if (entry.events.includes(event)) index += 1; 使得 index = 0 保持不变,index 从 0 增加到 2 因为 index += 2。此时表[索引] = 表[2]。当 table[2] += 1 运行并且表变为 [0, 0, 1, 0] 因为表转到索引 2 并将其设置为 1。

        C.在两个if条件为真的情况下,index=0在if(entry.events.includes(event )) index += 1;,并在 if (entry.squirrel) index += 2; 处进一步增加 2。即 index = 0 +1 +2 = 3。此时,index = 3 并且 table[index] = table[3]。因此,table[3] += 1 将 table 更新为 table[0, 0, 0, 1]

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

        希望对你有所帮助

        【讨论】:

          【解决方案5】:

          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.

          【讨论】:

            猜你喜欢
            • 2015-09-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-04-11
            • 2016-10-03
            相关资源
            最近更新 更多