Results 1 to 2 of 2

Thread: How can I rewrite this with Ternary Operator?

  1. #1
    Join Date
    Jan 2017
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question How can I rewrite this with Ternary Operator?

    HI,

    How can I rewrite this with Ternary Operator?

    Code:
    face.textContent = result == 0 ? headNr + ' times ' + resultFace : tailNr + ' times ' + resultFace;
    I tried everything, but couldn't find a way.

    Thanks

  2. #2
    Join Date
    Mar 2011
    Posts
    1,707
    Thanks
    59
    Thanked 102 Times in 100 Posts
    Blog Entries
    4

    Default

    Quote Originally Posted by kayut View Post
    HI,

    How can I rewrite this with Ternary Operator?

    Code:
    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?


    Code:
    	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.

  3. The Following User Says Thank You to keyboard For This Useful Post:

    kayut (10-04-2018)

Similar Threads

  1. Delete operator javascript
    By jscheuer1 in forum The lounge
    Replies: 5
    Last Post: 04-08-2016, 03:33 PM
  2. Subtraction operator not working!!
    By dcr33 in forum PHP
    Replies: 1
    Last Post: 05-27-2012, 10:00 AM
  3. Operator '?'
    By borris83 in forum PHP
    Replies: 5
    Last Post: 04-02-2009, 11:35 AM
  4. At A Glance Conditionals: Ternary Operators
    By rainarts in forum JavaScript
    Replies: 3
    Last Post: 07-26-2008, 04:45 AM
  5. && operator
    By noob_developer in forum JavaScript
    Replies: 1
    Last Post: 05-28-2008, 03:57 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •