0
if (props.name == "Computer"){
        if (result == "Won"){
            result = "Lost";
        }
        else if (result == "Lost"){
            result = "Won";
        }
    }

Tried converting to ternary, but failed to do so. (Have no idea how to mix up the first and second line). Also, not sure about "HAVE NO IDEA WHAT TO PUT" this spot.

result =="Won" ? result="Lost": result=="Lost" ? result="Won" : <HAVE NO IDEA WHAT TO PUT>;
2
  • Why you need ternery operator. Code is more readable in if-else block Commented Feb 4, 2021 at 15:10
  • just trying to practice ternary operator. Commented Feb 4, 2021 at 15:11

2 Answers 2

1

There are several different cases in your logic. Perhaps it's unclear to you. But this is what your attempts with the ternary operator have revealed:

  • props.name could be something other than "Computer". What should be printed then?
  • result could be something other than just "Won" or "Lost"

Anyway, let's build this ternary operator up:

result = props.name == "Computer" ? <YOUR CURRENT LOGIC> : <WHATEVER HAPPENS WHEN PROPS IS NOT COMPUTER>;

I am simply going to have the code return "result" as-is whenever we hit a case that was not defined by your if-else:

result = (props.name == "Computer" ? (result == "Won" ? "Lost" : "Won" ) : result);
Sign up to request clarification or add additional context in comments.

1 Comment

oh yea, I wanted to leave it as result if it doesn't meet the condition. it worked! thank you.
1
result = (props.name == "Computer" ? (result == "Won" ? "Lost" : "Won") : "What you want when props.name is not Computer");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.