7 Replies - 408 Views - Last Post: 03 October 2018 - 11:13 AM

#1 kayut   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 20-January 17

How can I rewrite this with Ternary Operator?

Posted 03 October 2018 - 09:39 AM

HI,

How can I rewrite this with Ternary Operator?

face.textContent = result == 0 ? headNr + ' times ' + resultFace : tailNr + ' times ' + resultFace;


I tried everything, but couldn't find a way.

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: How can I rewrite this with Ternary Operator?

#2 Programmer2004   User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 94
  • Joined: 25-October 17

Re: How can I rewrite this with Ternary Operator?

Posted 03 October 2018 - 09:46 AM

It's already written using the ternary operator.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7259
  • View blog
  • Posts: 15,141
  • Joined: 16-October 07

Re: How can I rewrite this with Ternary Operator?

Posted 03 October 2018 - 09:58 AM

Though it doe seem slightly longer than it has to be...
face.textContent = (result == 0 ? headNr : tailNr) + ' times ' + resultFace;


It is possible, I suppose, you might be running into convolution with you concatenation. You could try:
face.textContent = result == 0 
    ? (headNr + ' times ' + resultFace)
    : (tailNr + ' times ' + resultFace)
    ;


Was This Post Helpful? 1
  • +
  • -

#4 Programmer2004   User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 94
  • Joined: 25-October 17

Re: How can I rewrite this with Ternary Operator?

Posted 03 October 2018 - 10:02 AM

Oh, so this is the problem. Sorry for the wrong answer then, I misunderstood the question.
Was This Post Helpful? 1
  • +
  • -

#5 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2669
  • View blog
  • Posts: 10,657
  • Joined: 03-December 12

Re: How can I rewrite this with Ternary Operator?

Posted 03 October 2018 - 10:15 AM

face.textContent = (result == 0 ? headNr : tailNr) + ' times ' + resultFace;

Was This Post Helpful? 1
  • +
  • -

#6 kayut   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 20-January 17

Re: How can I rewrite this with Ternary Operator?

Posted 03 October 2018 - 10:44 AM

Thanks guys.

Is it also possible to rewrite this ternary operation with Template Literal?

I tried the following but it's not working:
face.textContent = `${result} == 0 ? ${headNr} times ${resultFace} : ${tailNr} times ${resultFace}`;

Was This Post Helpful? 0
  • +
  • -

#7 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7259
  • View blog
  • Posts: 15,141
  • Joined: 16-October 07

Re: How can I rewrite this with Ternary Operator?

Posted 03 October 2018 - 11:12 AM

You need the code in the curlies:
`${result == 0 ? headNr : tailNr} times ${resultFace}`


Was This Post Helpful? 1
  • +
  • -

#8 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2105
  • View blog
  • Posts: 6,443
  • Joined: 15-January 14

Re: How can I rewrite this with Ternary Operator?

Posted 03 October 2018 - 11:13 AM

I haven't tested this, but the entire expression needs to go inside the curly brackets.

face.textContent = `${${result} == 0 ? ${headNr} times ${resultFace} : ${tailNr} times ${resultFace}}`;


Again, I haven't tested that and I'm not sure off the top of my head if templates allow nested expressions like that, but I'll also say that if you're just setting that property to a string value then it doesn't seem like you need a template literal at all.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1