PDA

View Full Version : How can I rewrite this with Ternary Operator?



kayut
10-03-2018, 04:40 PM
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

keyboard
10-04-2018, 02:10 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

You're already using a ternary operator? I tested your code and it appeared to work fine for me.
What is exact problem you're having?




var headNr = "aaaa";
var resultFace = "bbbb";
var tailNr = "cccc";
var test = result == 1 ? headNr + ' times ' + resultFace : tailNr + ' times ' + resultFace;
console.log(test);

result=0
cccc times bbbb

result=1
aaaa times bbbb


Just a quick note. In situations like these its usually better to write out the full code so that it is easy to manage in the future, rather than compressing it down.