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

View all comments

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! :)