雄辩的javascript关联表解释 [英] eloquent javascript correlation tableFor explanation
问题描述
我正在迈出编程的第一步,我从雄辩中解决了这个问题,特别是对于weresquirrel问题。在这里:
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]
我明白了第一个function 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
我所理解的是,一个事件作为参数传递,它通过Jounral中的对象数组来查看它是否存在。但计数如何发生?
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.it继续:
so index is incremented and becomes 1.it continues to :
if (entry.squirrel) index += 2;
squirel是假的,所以没有任何索引。如果要找到squirel,为什么它+ 2 ???
squirel is false, so nothing happens to index.If squirel were to be found, why is it +2 ???
然后
表[index] + = 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 [3]
是 entry.squirrel
都是 hasEvent(事件)的条目数,条目)
是真的。 table [2]
是只有 entry.squirrel
为真的计数, table [1 ]
是只有 hasEvent(事件,条目)
为真的计数, 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
从<$ c开始$ C> 0 。如果 hasEvent(event,entry)
为true,我们将 1
添加到其中。如果 entry.squirrel
为true,我们将 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.
这篇关于雄辩的javascript关联表解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!