10 JSX Conditional

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
10 JSX conditionals, If statements that do work.

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
Coin toss doesn’t compile correctly. Can you find the what brackets or curly braces are incorrect?
Correct syntax is the word I’m looking for.



Replace this line with your code. 
var React = require('react');
var ReactDOM = require('react-dom');

if (user.age >= drinkingAge) {
  var message = (
    <h1>
      Hey, check out this alcoholic beverage!
    </h1>
  );
} else {
  var message = (
    <h1>
      Hey, check out these earrings I got at Claire's!
    </h1>
  );
  if (coinToss() == 'heads'){
  var img = <img src={pics.kitty} />; 
  }else{    
 var img = <img src={pics.doggy} />;    
     }
  
}

ReactDOM.render(
  message, 
  document.getElementById('app')
);

On the second part of the question it tell you to do two things but apparently you miss understood,

"At the bottom of the file, call ReactDOM.render.

For ReactDOM.render’s first argument, pass in img."

The first part of this question is:

  if (coinToss() == 'heads'){
  var img = <img src={pics.kitty} />; 
  }else{    
 var img = <img src={pics.doggy} />;    
     }

Not sure about the second one

hello i’m trying to solve the exercice n10 in React JSX Conditionnals i can’t find the solution
this is my code
if(coinToss() == “heads”) {
var img = (

);
}else{
var img = (

);
}
Chris
and i got this error message Your condition should be if (coinToss() == “heads”) {

Your condition should be if (coinToss() == “heads”) {

Still can’t find my problem for coin toss.
Syntax placement is a mystery.
Do I need parentheses?

Me too I didn’t find the solution I tried this but no success :slight_smile:

if(coinToss() == “heads”){
var img = (

);
}else{
var img = (

);
}

1 Like

Hi! That happened with me also.

The solution is to put the code exactly as the console tells you to do. Use a space after if and in -> “heads”) {

Also, it won’t accept it if you use var img = ( );
You have to use var img = <img ... /> ;

regards

2 Likes

It worked this way for me
if (coinToss() == “heads”) {
var img = ;
} else {
var img = ;
}
ReactDOM.render(
img,
document.getElementById(‘app’)
);

It is true, to make the first part of the exercise to work you need to have a space between if and the paranthesis :

if (statement) etc…

2 Likes

I found the problem thanks to you guys!
if() => cause the error
if () => this pass the compiler !
just need a “space” between if and ()

This worked for me:

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

function coinToss () {
  // This function will randomly return either 'heads' or 'tails'.
  return Math.random() < 0.5 ? 'heads' : 'tails';
}

var pics = {
  kitty: 'https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-kitty.jpg',
  doggy: 'https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-puppy.jpeg'
};

// if/else statement begins here:
if (coinToss() == "heads") {
  var img = <img src={pics.kitty} />;
} else {
  var img = <img src={pics.doggy} />;
}

ReactDOM.render(img, document.getElementById('app'));
1 Like

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