admin管理员组

文章数量:1278980

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

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
Share Improve this question edited Apr 19, 2018 at 23:31 Jason Aller 3,65228 gold badges41 silver badges39 bronze badges asked Apr 1, 2015 at 7:46 Shaik Md N RasoolShaik Md N Rasool 5001 gold badge6 silver badges13 bronze badges 0
Add a ment  | 

11 Answers 11

Reset to default 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;
} 

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

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

Here is a math trick

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

console.log(multiply(5,22))

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

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

}

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

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

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

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.

本文标签: