2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 5a24c314108439a4d4036183
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Use Advanced JavaScript in React Render Method
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								challengeType: 6
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 301415
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: use-advanced-javascript-in-react-render-method
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --description--
  
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								In previous challenges, you learned how to inject JavaScript code into JSX code using curly braces, `{ }` , 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.
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You can also write JavaScript directly in your `render`  methods, before the `return`  statement, ** *without*** 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 *inside*  the `return`  statement, you place the variable name inside curly braces.
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --instructions--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-02-25 09:19:24 -08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								In the code provided, the `render`  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 `ask`  method, so each time the button is clicked a random number will be generated and stored as the `randomIndex`  in state. On line 52, delete the string `change me!`  and reassign the `answer`  const so your code randomly accesses a different index of the `possibleAnswers`  array each time the component updates. Finally, insert the `answer`  const inside the `p`  tags.
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --hints--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The `MagicEightBall`  component should exist and should render to the page.
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.strictEqual(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Enzyme.mount(React.createElement(MagicEightBall)).find('MagicEightBall')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .length,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`MagicEightBall` 's first child should be an `input`  element. 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.strictEqual(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Enzyme.mount(React.createElement(MagicEightBall))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .children()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .childAt(0)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .name(),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  'input'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`MagicEightBall` 's third child should be a `button`  element. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.strictEqual(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Enzyme.mount(React.createElement(MagicEightBall))
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .children()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .childAt(2)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .name(),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  'button'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`MagicEightBall` 's state should be initialized with a property of `userInput`  and a property of `randomIndex`  both set to a value of an empty string. 
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Enzyme.mount(React.createElement(MagicEightBall)).state('randomIndex') ===
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    '' & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Enzyme.mount(React.createElement(MagicEightBall)).state('userInput') === ''
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								When `MagicEightBall`  is first mounted to the DOM, it should return an empty `p`  element.
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  Enzyme.mount(React.createElement(MagicEightBall)).find('p').length === 1 & & 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    Enzyme.mount(React.createElement(MagicEightBall)).find('p').text() === ''
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								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.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								(() => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const comp = Enzyme.mount(React.createElement(MagicEightBall));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const simulate = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    comp.find('input').simulate('change', { target: { value: 'test?' } });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    comp.find('button').simulate('click');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const result = () => comp.find('p').text();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _1 = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    simulate();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return result();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _2 = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    simulate();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return result();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _3 = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    simulate();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return result();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _4 = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    simulate();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return result();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _5 = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    simulate();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return result();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _6 = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    simulate();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return result();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _7 = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    simulate();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return result();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _8 = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    simulate();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return result();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _9 = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    simulate();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return result();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _10 = () => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    simulate();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return result();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  };
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _1_val = _1();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _2_val = _2();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _3_val = _3();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _4_val = _4();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _5_val = _5();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _6_val = _6();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _7_val = _7();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _8_val = _8();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _9_val = _9();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const _10_val = _10();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const actualAnswers = [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    _1_val,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    _2_val,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    _3_val,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    _4_val,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    _5_val,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    _6_val,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    _7_val,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    _8_val,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    _9_val,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    _10_val
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const hasIndex = actualAnswers.filter(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    (answer, i) => possibleAnswers.indexOf(answer) !== -1
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const notAllEqual = new Set(actualAnswers);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  assert(notAllEqual.size > 1 & &  hasIndex.length === 10);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								})();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --seed--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## --after-user-code--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```jsx
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								var 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',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  'Yes',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  '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',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  'Outlook not so good',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  'Very doubtful',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  'Most likely'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								ReactDOM.render(< MagicEightBall  / > , document.getElementById('root'));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## --seed-contents--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```jsx
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const inputStyle = {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  width: 235,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  margin: 5
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class MagicEightBall extends React.Component {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  constructor(props) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    super(props);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.state = {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      userInput: '',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      randomIndex: ''
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    };
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    this.ask = this.ask.bind(this);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.handleChange = this.handleChange.bind(this);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ask() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (this.state.userInput) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      this.setState({
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        randomIndex: Math.floor(Math.random() * 20),
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        userInput: ''
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								      });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  handleChange(event) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.setState({
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      userInput: event.target.value
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  render() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const possibleAnswers = [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'It is certain',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'It is decidedly so',
							 
						 
					
						
							
								
									
										
										
										
											2018-10-08 01:01:53 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      'Without a doubt',
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								      'Yes, definitely',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'You may rely on it',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'As I see it, yes',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Outlook good',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Yes',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Signs point to yes',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Reply hazy try again',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Ask again later',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Better not tell you now',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Cannot predict now',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Concentrate and ask again',
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      "Don't count on it",
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								      'My reply is no',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'My sources say no',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Most likely',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Outlook not so good',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Very doubtful'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    ];
							 
						 
					
						
							
								
									
										
										
										
											2020-09-15 09:53:25 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    const answer = 'change me!'; // Change this line
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    return (
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      < div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        < input 
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								          type='text'
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								          value={this.state.userInput}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          onChange={this.handleChange}
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								          style={inputStyle}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        />
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        < br  / > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        < button  onClick = {this.ask} > Ask the Magic Eight Ball!< / button > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        < br  / > 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        < h3 > Answer:< / h3 > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        < p > 
							 
						 
					
						
							
								
									
										
										
										
											2020-09-15 09:53:25 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								          {/* Change code below this line */}
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-09-15 09:53:25 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								          {/* Change code above this line */}
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        < / p > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      < / div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-13 18:58:50 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```jsx
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								const inputStyle = {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  width: 235,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  margin: 5
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								};
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class MagicEightBall extends React.Component {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  constructor(props) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    super(props);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.state = {
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      userInput: '',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      randomIndex: ''
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    };
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    this.ask = this.ask.bind(this);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.handleChange = this.handleChange.bind(this);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  ask() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (this.state.userInput) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      this.setState({
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        randomIndex: Math.floor(Math.random() * 20),
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        userInput: ''
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								      });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  handleChange(event) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    this.setState({
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      userInput: event.target.value
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  render() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const possibleAnswers = [
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      'It is certain',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'It is decidedly so',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Without a doubt',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Yes, definitely',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'You may rely on it',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'As I see it, yes',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Outlook good',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Yes',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      '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',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Outlook not so good',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Very doubtful',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      'Most likely'
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    ];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const answer = possibleAnswers[this.state.randomIndex];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return (
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      < div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        < input 
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								          type='text'
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								          value={this.state.userInput}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          onChange={this.handleChange}
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								          style={inputStyle}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        />
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        < br  / > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        < button  onClick = {this.ask} > Ask the Magic Eight Ball!< / button > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        < br  / > 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        < h3 > Answer:< / h3 > 
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        < p > {answer}< / p > 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								      < / div > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2020-09-03 01:24:22 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```