0

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
0

11 Answers 11

7

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;
} 
3
  • 2
    Explaining the downvote is both good for me and my code as both gets improved.
    – Amit Joki
    Apr 1, 2015 at 8:41
  • @ShaikRasool why do you think I know it?
    – Amit Joki
    Apr 1, 2015 at 9:11
  • 1
    multiply(5,0) it will give 5 instead of 0
    – Aniket
    Dec 4, 2021 at 6:21
3

a=(b,c)=>Math.round(b/(1/c))

4
  • 3
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
    – dferenc
    Feb 10, 2018 at 19:59
  • dferenc, I believe it's pretty much straightforward except maybe round method — I used it in order to get sane and accurate result e.g. (since 0.1 + 0.2 !== 0.3)
    – daGo
    Feb 11, 2018 at 10:46
  • 1
    It is a great solution indeed, and I did not flag it or anything (note: code only answers always go to review queue…). However, I guess even a simple arrow function can be something not trivial for someone who just started to learn javascript.
    – dferenc
    Feb 11, 2018 at 11:42
  • it's also not trivial for people not familiar with/rusty with reciprocals. Apr 19, 2018 at 22:21
2

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;
  }
}
1

Here is a math trick

function multiply(num1, num2) {
    return num1/(1/num2);
}

console.log(multiply(5,22))

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 26, 2021 at 15:25
0

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 :(

0

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);

}

0

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.

0

solution in c#:

public int multiply(int a, int b, int sum = 0)
{
    if (a == 0)
     return 0;
    if (a == 1)
     return sum + b;
    if (a == -1)
     return -(sum + b);
    if (a > 0)
     a--;
    else
     a++;
       
    sum += b;
     return sum

    multiply(a, b, sum);
}
1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Nov 9, 2023 at 7:34
-1

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));

1
  • Please add more context to your code, what are you trying to achieve? What is not happening? Aug 5, 2019 at 1:16
-1
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;
    }
}
-2

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.