title: Use Advanced JavaScript in React Render Method
challengeType: 6
isRequired: false
---
## Description
<sectionid='description'>
In previous challenges, you learned how to inject JavaScript code into JSX code using curly braces, <code>{ }</code>, for tasks like accessing props, passing props, accessing state, inserting comments into your code, and most recently, styling your components. These are all common use cases to put JavaScript in JSX, but they aren't the only way that you can utilize JavaScript code in your React components.
You can also write JavaScript directly in your <code>render</code> methods, before the <code>return</code> statement, <strong><em>without</em></strong> inserting it inside of curly braces. This is because it is not yet within the JSX code. When you want to use a variable later in the JSX code <em>inside</em> the <code>return</code> statement, you place the variable name inside curly braces.
</section>
## Instructions
<sectionid='instructions'>
In the code provided, the <code>render</code> method has an array that contains 20 phrases to represent the answers found in the classic 1980's Magic Eight Ball toy. The button click event is bound to the <code>ask</code> method, so each time the button is clicked a random number will be generated and stored as the <code>randomIndex</code> in state. On line 52, delete the string <code>"change me!"</code> and reassign the <code>answer</code> const so your code randomly accesses a different index of the <code>possibleAnswers</code> array each time the component updates. Finally, insert the <code>answer</code> const inside the <code>p</code> tags.
- text: The <code>MagicEightBall</code> component should exist and should render to the page.
testString: 'assert.strictEqual(Enzyme.mount(React.createElement(MagicEightBall)).find(''MagicEightBall'').length, 1, ''The <code>MagicEightBall</code> component should exist and should render to the page.'');'
- text: <code>MagicEightBall</code>'s first child should be an <code>input</code> element.
testString: 'assert.strictEqual(Enzyme.mount(React.createElement(MagicEightBall)).children().childAt(0).name(), ''input'', ''<code>MagicEightBall</code>'s first child should be an <code>input</code> element.'');'
- text: <code>MagicEightBall</code>'s third child should be a <code>button</code> element.
testString: 'assert.strictEqual(Enzyme.mount(React.createElement(MagicEightBall)).children().childAt(2).name(), ''button'', ''<code>MagicEightBall</code>'s third child should be a <code>button</code> element.'');'
- text: <code>MagicEightBall</code>'s state should be initialized with a property of <code>userInput</code> and a property of <code>randomIndex</code> both set to a value of an empty string.
testString: 'assert(Enzyme.mount(React.createElement(MagicEightBall)).state(''randomIndex'') === '''' && Enzyme.mount(React.createElement(MagicEightBall)).state(''userInput'') === '''', ''<code>MagicEightBall</code>'s state should be initialized with a property of <code>userInput</code> and a property of <code>randomIndex</code> both set to a value of an empty string.'');'
- text: 'When <code>MagicEightBall</code> is first mounted to the DOM, it should return an empty <code>p</code> element.'
testString: 'assert(Enzyme.mount(React.createElement(MagicEightBall)).find(''p'').length === 1 && Enzyme.mount(React.createElement(MagicEightBall)).find(''p'').text() === '''', ''When <code>MagicEightBall</code> is first mounted to the DOM, it should return an empty <code>p</code> element.'');'
- text: 'When text is entered into the <code>input</code> element and the button is clicked, the <code>MagicEightBall</code> component should return a <code>p</code> element that contains a random element from the <code>possibleAnswers</code> array.'