Ternary Operator not working


#1


https://www.codecademy.com/en/courses/react-101/lessons/react-components-advanced-jsx/exercises/render-function-if-statement?action=lesson_resume&link_content_target=interstitial_lesson

 SyntaxError: /home/ccuser/workspace/learn-react-02-02-04d/app.js: Unexpected token (9:29)
 7 | var TonightsPlan = React.createClass({
 8 |   render: function() {
 >  9 |     var target = {fiftyFifty ? 'out' : 'to bed' };
 |                              ^
10 |     
11 |     return <h1>Tonight I'm going {target} WOOO</h1>;
12 |   }
at Parser.pp.raise (/usr/lib/node_modules/babelify/node_modules/babylon/lib/parser/location.js:22:13)
at Parser.pp.unexpected (/usr/lib/node_modules/babelify/node_modules/babylon/lib/parser/util.js:89:8)
at Parser.pp.expect (/usr/lib/node_modules/babelify/node_modules/babylon/lib/parser/util.js:83:33)
at Parser.pp.parseObj (/usr/lib/node_modules/babelify/node_modules/babylon/lib/parser/expression.js:725:12)
at Parser.pp.parseExprAtom (/usr/lib/node_modules/babelify/node_modules/babylon/lib/parser/expression.js:486:19)
at Parser.parseExprAtom (/usr/lib/node_modules/babelify/node_modules/babylon/lib/plugins/jsx/index.js:18:22)
at Parser.pp.parseExprSubscripts (/usr/lib/node_modules/babelify/node_modules/babylon/lib/parser/expression.js:272:19)
at Parser.pp.parseMaybeUnary (/usr/lib/node_modules/babelify/node_modules/babylon/lib/parser/expression.js:252:19)
at Parser.pp.parseExprOps (/usr/lib/node_modules/babelify/node_modules/babylon/lib/parser/expression.js:183:19)
at Parser.pp.parseMaybeConditional   (/usr/lib/node_modules/babelify/node_modules/babylon/lib/parser/expression.js:165:19)


var React = require('react');
var ReactDOM = require('react-dom');

var fiftyFifty = Math.random() < 0.5;

// React.createClass call begins here:
var TonightsPlan = React.createClass({
  render: function() {
    var target = {fiftyFifty ? 'out' : 'to bed' };
    
    return <h1>Tonight I'm going {target} WOOO</h1>;
  }
});


#2

That's not a valid expression.

Here's a simpler version of what you did:

var x = { 5 };

That's not valid javascript. Perhaps you meant to do:

var x = 5;

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.