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

Show parent comments

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.