I am trying to figure out ways to get the products of two integers without using * in my solution. The closest I got was is
/*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))
You could take some bit shifting, known as ancient egyptian multiplication or russian multiplication.
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)); // 48
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With