eloquent javascript相关表解释 [英] eloquent javascript correlation tableFor explanation
问题描述
我正在迈出编程的第一步,但我从 eloquent 开始就被这个问题困住了,尤其是在 wassquirrel 问题上.在这里:
I am taking my first steps in programming and i am stuck with this problem from eloquent, in particular with the weresquirrel problem. Here it goes :
function hasEvent(event, entry) {
return entry.events.indexOf(event) != -1;
}
function tableFor(event, journal) {
var table = [0, 0, 0, 0];
for (var i = 0; i < journal.length; i++) {
var entry = journal[i], index = 0;
if (hasEvent(event, entry)) index += 1;
if (entry.squirrel) index += 2;
table[index] += 1;
}
return table;
}
console.log(tableFor("pizza", JOURNAL));
// → [76, 9, 4, 1]
我确实理解第一个函数 hasevent,如果条目包含给定事件,它返回 true.
I do understand the first function hasevent, it returns true if an entry contains a given event.
我无法理解的是 tableFor 函数.我不知道函数是如何流动的,以及表是如何获得它的值的.例如对于 console.log(tableFor("pizza", JOURNAL));,我们得到 [76, 9, 4, 1].但是看在上帝的份上怎么办?
what i can't grasp is the tableFor function. I can't get how the function flows and HOW the table gets its values. For example for console.log(tableFor("pizza", JOURNAL)); , we get [76, 9, 4, 1]. But How for god's sake ?
期刊由书提供,看起来像这样:
The journal is supplied by the book and looks like this :
var JOURNAL = [
{"events":["pizza","exercise","weekend"],"squirrel":false},
{"events":["bread","pudding","brushed teeth","weekend","touched
tree"],"squirrel":false},
{"events":["carrot","nachos","brushed
teeth","cycling","weekend"],"squirrel":false},
{"events":["brussel sprouts","ice cream","brushed
teeth","computer","weekend"],"squirrel":false},
{"events":["potatoes","candy","brushed
teeth","exercise","weekend","dentist"],"squirrel":false},
{"events":["brussel sprouts","pudding","brushed
teeth","running","weekend"],"squirrel":false},
{"events":["pizza","brushed teeth","computer","work","touched
tree"],"squirrel":false},
{"events":["bread","beer","brushed
teeth","cycling","work"],"squirrel":false},
{"events":["cauliflower","brushed teeth","work"],"squirrel":false},
{"events":["pizza","brushed teeth","cycling","work"],"squirrel":false},
{"events":["lasagna","nachos","brushed teeth","work"],"squirrel":false},
{"events":["brushed teeth","weekend","touched tree"],"squirrel":false},
{"events":["lettuce","brushed
teeth","television","weekend"],"squirrel":false},
{"events":["spaghetti","brushed teeth","work"],"squirrel":false},
{"events":["brushed teeth","computer","work"],"squirrel":false},
{"events":["lettuce","nachos","brushed teeth","work"],"squirrel":false},
{"events":["carrot","brushed teeth","running","work"],"squirrel":false} ...etc
我的理解是,事件作为参数传递,它会查看日志中的对象数组以查看它是否存在.但是计数是如何进行的呢?
What i understand is that an event is passed as a parameter and it looks through the array of objects in Jounral to see if it's present. But how does the counting take place ?
if (entry.squirrel) index += 2;
为什么是 +2 ?为什么不索引 += 3;或指数 += 4;???
Why is this +2 ? why not index += 3; or index += 4; ???
最后为什么 table[index] += 1;???
and finally why table[index] += 1; ???
例如第一个循环是这样的,对于:console.log(tableFor("pizza", JOURNAL));
For example the first loop goes like this, for : console.log(tableFor("pizza", JOURNAL));
//FLOW
i=0;
从日志上面的第一行开始,披萨是存在的.
from the first line above in journal, pizza is present.
if (hasEvent(event, entry)) index += 1;
所以索引增加并变成1.它继续:
so index is incremented and becomes 1.it continues to :
if (entry.squirrel) index += 2;
squirel 是假的,所以索引没有任何变化.如果找到了松鼠,为什么它是 +2 ???
squirel is false, so nothing happens to index.If squirel were to be found, why is it +2 ???
然后表[索引] += 1从这一点上我无法理解.
then table[index] += 1 i can't understand from this point.
谁能帮我分解一下?这对我的训练很有帮助.
Can someone please break it down for me ? It would be very helpful for my training.
先谢谢你.
推荐答案
table
数组是满足不同条件的条目的计数.table[3]
是 entry.squirrel
和 hasEvent(event, entry)
都为真的条目数.table[2]
是 entry.squirrel
为真的计数,table[1]
是 hasEvent(event, entry)
为真,并且 table[0]
是两者都不为真的计数.
The table
array is a count of entries that meet different criteria. table[3]
is the count of entries where both entry.squirrel
are hasEvent(event, entry)
are true. table[2]
is the count where just entry.squirrel
is true, table[1]
is the count where just hasEvent(event, entry)
is true, and table[0]
is the count where neither is true.
所以逻辑是 index
从 0
开始.如果 hasEvent(event, entry)
为真,我们添加 1
到它.如果 entry.squirrel
为真,我们添加 2
到它.结果是,如果两者都为真,我们最终添加了 3
.如果两者都不是,我们就不添加任何东西,所以它仍然是 0
.
So the logic is that index
starts at 0
. If hasEvent(event, entry)
is true, we add 1
to it. If entry.squirrel
is true we add 2
to it. The result is that if both are true, we end up adding 3
. And if neither is true we don't add anything, so it's still 0
.
然后我们将 1
添加到 table[index]
以增加该计数器.
Then we add 1
to table[index]
to increment that counter.
这篇关于eloquent javascript相关表解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!