-6

I am trying to shorten my code with this conditional statement

I have

  if(title =='project'){
      title = text + ': ' + this.projectTitle;
  }else{
      title = this.projectTitle;
  }

I feel like there is a better way to rewrite this. Does anyone have idea of how to do it?

Thanks!

FlyingCat
  • 14,036
  • 36
  • 119
  • 198
  • 4
    http://codereview.stackexchange.com/ – Ant P Sep 17 '13 at 17:01
  • That looks pretty short already. Why try to shorten it? *Edit:* And the three current answers demonstrate starkly that you’re not going to end up with something more readable than the original. – bdesham Sep 17 '13 at 17:01
  • So, I'm *fascinated* by the serial, unexplained, down-voting... – David Thomas Sep 17 '13 at 17:03
  • 6
    @DavidThomas I downvoted because the OP is trying to shorten a piece of code that is already trivially short. (And while this may not be fair, I think people expect more “research effort” and a more useful question from a user with 3k reputation.) – bdesham Sep 17 '13 at 17:05
  • have to agree with @bdesham, SO isn't a place to go and learn what a ternary operator is; that would be a tutor, book or website – jenson-button-event Sep 17 '13 at 17:07
  • 2
    While I'm @DavidThomas, fascinated by the serial, unexplained, [up-voting](http://stackoverflow.com/questions/18347033/how-to-shorten-my-conditional-statements). – gdoron Sep 17 '13 at 17:14
  • I understand the question is very bad. It was a bad post and I apologize. Thanks all – FlyingCat Sep 17 '13 at 17:15
  • 1
    Down-voting, though, implies that the answer is 'not useful,' which I realise is a subjective determination (though to my mind should be 'does not answer, or address, the question'); none of the posted answers failed to address, or solve, the question as asked. So it feels like those that answered are being punished simply because someone found the *question* lacked merit. Obviously down-votes are given at the whims of individual users (and I have no problem *being* down-voted, but I'd like some explanation as to *why* I'm being down-voted, so thanks for that at least). – David Thomas Sep 17 '13 at 19:49
  • 3
    This question appears to be off-topic because it is about [codereview](http://codereview.stackexchange.com/). – Sirko Sep 18 '13 at 09:40

5 Answers5

1

Try a ternary:

title = (title =='project') ? text + ': ' + this.projectTitle: this.projectTitle;
j08691
  • 204,283
  • 31
  • 260
  • 272
1

A ternary that reproduces your if/else:

title = title == 'project' ? text + ': ' + this.projectTitle : this.projectTitle;

Though if you really want to shorten it:

title = (title == 'project' ? text + ': ' : '') + this.projectTitle;

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
1

Simple inline condition;

   title =  ((title =='project') ? text + ': ' + this.projectTitle : this.projectTitle);

Check link for more details

Community
  • 1
  • 1
Nadeem_MK
  • 7,533
  • 7
  • 50
  • 61
1
title = (title == 'project' ? text + ': ' : '') + this.projectTitle;
Paul
  • 139,544
  • 27
  • 275
  • 264
1
var title = (title =='project' ? text + ': ' + this.projectTitle : this.projectTitle);
yPhil
  • 8,049
  • 4
  • 57
  • 83