var y= '110001'.split("").reverse();
var sum = 0;
for (var i = 0; i < y.length; i++) {
sum += (y[i] * Math.pow(2, i));
}
console.log(sum);
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
It would be simplest to do
console.log(Array.from('110001').reduce((prev, cur) => prev << 1 | cur));
<<
is the left-bitshift operator, which here essentially multiplies by two.
Array.from
(if available) is preferable to split
. In this case it doesn't matter, but split
will fail with surrogate pair characters such as