r/javaScriptStudyGroup • • May 10 '16

CHALLENGE: Write a multiplication function...

CHALLENGE:

Write a multiplication function that takes two numbers integers as arguments and returns their product.

Rules:

  1. Cannot use * or / operators

  2. Cannot use Math object

  3. Cannot use for (any variation) or while (including do) loops

2 Upvotes

17 comments sorted by

2

u/Volv May 10 '16 edited May 10 '16

Not gonna look at yours for a bit cause there must be more ways to do this.
Do you consider this cheating? :)

1

u/ForScale May 10 '16

Nice! I came up with two ways to do it. Both use a similar approach to avoid loops, but one uses a predefined method where the other does not.

I don't consider a blank Pen to be cheating, but I also don't consider it to be a valid answer! ;) I think you forgot to click save...

2

u/Volv May 10 '16

How about this

1

u/ForScale May 10 '16

Aha! No, I don't consider that cheating. That fill method... nice!! Wait till you see my solutions, the first one certainly could have used fill. Thanks for the heads-up on it!

And whats the 0 argument in the reduce function? Is that starting index?

2

u/Volv May 10 '16

yep, exactly.

1

u/ForScale May 10 '16

Cool. Nice work with the challenge and the one-liner! It just came to me for some reason so I thought I'd post it here. Let me know your thoughts on my solution when you get a chance! Obviously no rush or anything.

2

u/Volv May 10 '16 edited May 10 '16

Recursion Example. Fixed signs this time Codepen
 
Although I used a multiply symbol for those signs... hmm
And now I got rid of it

2

u/ForScale May 10 '16

Nice!

Your syntax is getting interesting... Is it common practice to ditch the curly braces with if statements? Or is that a babel thing?

1

u/Volv May 10 '16

Ternary is fairly common for simple if then else when all you are doing is assigning values. That one is nested but I think formatted in such a way that it is obvious enough without being too hard to read?

  var resultSign = (x < 0) ? // x less than zero
                   (y > 0) ? -1 : 1
                   :         // x greater than zero
                   (y < 0) ? -1 : 1;  

 
versus

  var resultSign = 0;

  if (x < 0) {
    if (y > 0) {
      resultSign = -1;
    } else {
      resultSign = 1;
    }
  } else {
    if (y < 0) {
      resultSign = -1;
    } else {
      resultSign = 1;
    }
  }

1

u/ForScale May 10 '16

:) I'm not that bad! I know ternary, but I don't use it much and I don't think I've ever used it nested like that. Pretty cool!

No, I was talking about current line 29 and 30.

2

u/Volv May 10 '16

Generally advice is to not ditch them. I make exceptions for things like a quick check to break or return..
and in my defence I ditched wayyy more brackets with the ternarys :) Take it you found that ok enough to read. I worry when i start nesting them

1

u/ForScale May 10 '16

Yeah, ternaries were fine! I'm used to not seeing brackets with those.

1

u/ForScale May 10 '16

2

u/Volv May 10 '16 edited May 10 '16

Nice. straight for the recursion. I see what you mean about lack of array.fill :) It is another new ES6 thing and according to this doesn't work natively in IE atm. yay babel.
 
We have pretty similar answers for both it seems. Main difference in second one is you carrying the result (and iteration info) in a closure over main function instead of threaded through the function calls.
 
Success tho - Only issue is your first solution will fail for negative values of a.. but having said that mine fails for negative y :)

2

u/ForScale May 10 '16

Whoops! Didn't consider how a negative a would affect the reduce functionality. I think I fixed it though!

2

u/Volv May 10 '16

Seems indeed to be fixed :)

2

u/ForScale May 10 '16

Excellent! :)