Say*_*ran 3 javascript math integer
我试图找出在我的解决方案中不使用*而获得两个整数的产品的方法.我得到的最接近的是
/*Example: 2,5 output ====> 10 */
/*Example: 10,5 output ====> 50 */
const productOfTwoInt = (int,int2) => {
var data1 = 0;
var data2 = 0;
var result;
var result2;
for(var x = 0; x <= int; x++) {
result = x += data1
console.log(result)
}
for(var j = 0; j <= int2; j++) {
result2 = j += data2
}
return result + result2
}
console.log(productOfTwoInt(3,5))
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)a b p comment ---- ---- ---- ---------------------------------- 6 8 0 skip, because a is even 3 16 16 add 16 to p, because a is odd 1 32 48 add 32 to p, because a is odd 0 64 48 stop iteration, because a is zero
function product(a, b) {
var p = 0;
while (a) {
p += (a & 1) && b;
a >>= 1;
b <<= 1;
}
return p;
}
console.log(product(6, 8)); // 48Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
102 次 |
| 最近记录: |