'Can I perform multiplication without using the multiplication operator "*" in JavaScript
I need to multiply two numbers in JavaScript, but I need to do without using the multiplication operator "*". Is it possible?
function a(b,c){
return b*c;
} // note:need to do this without the "*" operator
Solution 1:[1]
Yes. Because multiplication is just addition done multiple times. Also have meaningful signatures for methods instead of using single alphabets.
function multiply(num, times){
// TODO what if times is zero
// TODO what if times is negative
var n = num;
for(var i = 1; i < times; i++)
num += n; // increments itself
return num;
}
Solution 2:[2]
a=(b,c)=>Math.round(b/(1/c))
Solution 3:[3]
You need to be able to handle negatives and zeros. Other above answers don't help here. There are different ways. One relatively messy way could be ifs:
function multiply(num1, num2) {
var sum = 0;
for (var i = 0; i < Math.abs(num2); i++) {
sum += num1;
}
if (num1 < 0 && num2 < 0) {
return Math.abs(sum);
} else if (num1 < 0 || num2 < 0 ) {
return -sum;
} else {
return sum;
}
}
Solution 4:[4]
There is another simpler mathematical approach. Let's do this in C++:
double mult(double a, double b) {
return exp(log(a) + log(b));
}
The log() function in C++ returns the natural logarithm (base-e logarithm) of the argument passed in the parameter. (arguments can be any numeric type)
The exp() function in C++ returns the exponential (Euler's number) e (or 2.71828) raised to the given argument.
When you simplify the above statement, you eventually end up with a * b, but still there is no * sign.
*You need make sure both a and b are positive, otherwise you will get nan :(
Solution 5:[5]
Here is a math trick
function multiply(num1, num2) {
return num1/(1/num2);
}
console.log(multiply(5,22))
Solution 6:[6]
I think this can be solved using recursion. Sorry for the improper indentations. We are given 2 numbers to multiply and multiplying m with n simply means adding m, n times.
if n becomes 0, return 0. This is our base case. else return m + multi(m,n-1)
we are returning m every time, because we need to add m up to n times In every call we are decreasing the n's value so as the n becomes 1, we'll call it for the last time.
function multi (int m, int n){
if(n === 0)
return 0;
return m + multi(m,n-1);
}
Solution 7:[7]
repeat() method of string can be used to find multiplication of two numbers.
var a = 3;
var b = 4;
var res = "1".repeat(a).repeat(b).length;
console.log(res)
log: 12
It is repeating c, a times=> 'ccc' and then whole string b times=> 'cccccccccccc', length of the final string will be a*b;
This is similar to loop approach. This approach is limited to positive and integer numbers only.
Solution 8:[8]
function multiply(num1, num2) {
let num = 0;
// Check whether one or both nums are negative
let flag = false;
if(num1 < 0 && num2 < 0){
flag = true;
// Make both positive numbers
num1 = Math.abs(num1);
num2 = Math.abs(num2);
}else if(num1 < 0 || num2 < 0){
flag = false;
// Make the negative number positive & keep in num2
if(num1 < 0){
temp = num2;
num2 = Math.abs(num1);
num1 = temp;
}else{
num2 = Math.abs(num2);
}
}else{
flag = true;
}
let product = 0;
while(num < num2){
product += num1;
num += 1;
}
// Condition satisfy only when 1 num is negative
if(!flag){
return -product;
}
return product;
}
console.log(multiply(-2,-2));
Solution 9:[9]
function multiple(a, b) {
let sum = 0;
for (let i = 0; i < Math.abs(b); i++) {
sum += Math.abs(a);
}
if (a < 0 && b < 0) {
return Math.abs(sum);
} else if (a < 0 || b < 0 ) {
return -sum;
} else {
return sum;
}
}
Solution 10:[10]
Is this from some programming puzzle or interview question? :)
Since multiplication is repeated addition, you probably want a loop which adds one of the factors to the result for each count in the other factor.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
