There is a bug Use Advanced JavaScript in React Render Method lesson

I think it’s should have work but get this two error:

  • Failed:When MagicEightBall is first mounted to the DOM, it should return an empty p element.

  • Failed:When text is entered into the input element and the button is clicked, the MagicEightBall component should return a p element that contains a random element from the possibleAnswers array.

Here is the solution that should work:

const inputStyle = {
  width: 235,
  margin: 5

class MagicEightBall extends React.Component {
  constructor(props) {
    this.state = {
      userInput: '',
      randomIndex: ''
    this.ask = this.ask.bind(this);
    this.handleChange = this.handleChange.bind(this);
  ask() {
    if (this.state.userInput) {
        randomIndex: Math.floor(Math.random() * 20),
        userInput: ''
  handleChange(event) {
  render() {
    const possibleAnswers = [
      'It is certain',
      'It is decidedly so',
      'Without a doubt',
      'Yes, definitely',
      'You may rely on it',
      'As I see it, yes',
      'Outlook good',
      'Signs point to yes',
      'Reply hazy try again',
      'Ask again later',
      'Better not tell you now',
      'Cannot predict now',
      'Concentrate and ask again',
      "Don't count on it",
      'My reply is no',
      'My sources say no',
      'Most likely',
      'Outlook not so good',
      'Very doubtful'
   const answer = possibleAnswers[this.state.randomIndex]; // Change this line
    return (
        <br />
        <button onClick={this.ask}>Ask the Magic Eight Ball!</button>
        <br />
          {/* Change code below this line */}
          {/* Change code above this line */}

Yes i see the embeded p tag. Now work!

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