【发布时间】:2018-08-27 06:47:17
【问题描述】:
我想与您分享我对这段代码的看法:
for (var i = 1, max = 5; i < max; i++) {
let random = Math.random();
let expression = (random < 1 / (i + 1));
if (expression){
console.log('random is: ' + random + ' and the expression is: ' + expression + ', i is: ' + i);
}else{
console.log('random was: ' + random + ' and the expression was: ' + expression + ', i was: ' + i);
}
}
我正在研究这个取自 GitHub 的示例:https://github.com/chuckha/ColorFlood
而且我很难知道 if() 中的表达式的含义。
我使用了 JS repl:https://jscomplete.com/repl/
这个例子的上下文是,这个函数将使用一个从 0 到 5 的随机索引,以将随机颜色映射到一个节点。
这里有一个来自 repl 的示例输出:
"random was: 0.7118559117992413 and the expression was: false, i was: 1"
"random was: 0.478919411809795 and the expression was: false, i was: 2"
"random was: 0.4610390397998597 and the expression was: false, i was: 3"
"random was: 0.7051121468181564 and the expression was: false, i was: 4"
【问题讨论】:
标签: javascript if-statement random colors nodes