How can I rewrite this with Ternary Operator?

Page 1 of 1

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

#1 kayut

Reputation: 0
• 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

Reputation: 18
• Posts: 96
• 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.

#3 baavgai

• Dreaming Coder

Reputation: 7327
• Posts: 15,237
• 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)
;

```

#4 Programmer2004

Reputation: 18
• Posts: 96
• 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.

#5 astonecipher

• Senior Systems Engineer

Reputation: 2688
• Posts: 10,767
• 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;
```

#6 kayut

Reputation: 0
• 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}`;
```

#7 baavgai

• Dreaming Coder

Reputation: 7327
• Posts: 15,237
• 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}`

```

#8 ArtificialSoldier

• D.I.C Lover

Reputation: 2187
• Posts: 6,616
• 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.