js & bitwise operator
js & bitwise operator
bitwise operator
https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/161#issuecomment-521172471
https://github.com/xgqfrms/learn-javascript-with-mdn/issues/8
demo
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2019-08-14
*
* @description auto-sevent-times-without-math.js
* @description 不用加减乘除运算符, 求整数的7倍
* @description js array get sum value without call math methods
* @augments
* @example eval([1,2,3].join('+'));// 6
* @link https://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-numbers
*
*/
let log = console.log;
const autoSevenTimes = (int = 0, times = 7, debug = false) => {
let result = [];
if (!int) {
return 0;
} else {
for (let i = 0; i < times; i++) {
result.push(int);
}
}
// result = result.reduce((acc, item) => acc + item);
result = eval(result.join(`+`));
if(debug) {
log(`result = `, result);
}
return result;
};
let num = 3;
autoSevenTimes(num);
// expect: 3 * 7 = 21
bitwise operator
https://repl.it/@xgqfrms/bitwise-operator-and-left-shift-and-right-shift
const autoSeventTimes = (num = 0, times = 7) => {
let x = Math.floor(times / 2);
return (num << x) - num;
};
let xyz = autoSeventTimes(3);
// 21
console.log(`xyz =`, xyz);
typed array
I think using typed array is more meaningful.
let result = [].concat(...([...new Uint8Array(num).map(item => item = 1)].map(x => [...new Uint8Array(7).map(item => item = 1)])));
console.log(result, result.length);
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/11563020.html
未经授权禁止转载,违者必究!