Guest User

Untitled

a guest
Oct 20th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.68 KB | None | 0 0
  1. var Utilities = (function (array, maxN) {
  2.  
  3. function generateRandomNumber(array, maxN) {
  4. let randomN = Math.floor(Math.random() * maxN) + 0;
  5. console.log(array.length);
  6. console.log(maxN);
  7.  
  8. if(array.length == maxN) {
  9. array.length = 0;
  10. }
  11.  
  12. if(!array.includes(randomN)) {
  13. array.push(randomN);
  14. }
  15. else {
  16. randomN = generateRandomNumber(array, maxN);
  17. }
  18. return randomN;
  19. }
  20.  
  21. return {
  22. generateRandomNumber: generateRandomNumber
  23. };
  24.  
  25.  
  26. })();
  27.  
  28. export default Utilities;
  29.  
  30. var cityArray = [];
  31.  
  32. function getRandomNumber() {
  33. let randomN = Utilities.generateRandomNumber(cityArray, 10);
  34. return randomN;
  35. }
  36.  
  37. class App extends Component {
  38. constructor(props) {
  39. super(props);
  40.  
  41. this.maxImgArray = [];
  42. this.array = [];
  43. this.maxImg = 10;
  44.  
  45. this.state = {
  46. visibility: {
  47. form: true,
  48. categories: false,
  49. grid: false,
  50. timeUp: false,
  51. city: false
  52. },
  53. data: "",
  54. points: 0,
  55. h1: false,
  56. bgColor: "",
  57. catBgColor: "",
  58. bgImg: "",
  59. finalPlay: {
  60. correct: false,
  61. incorrect: false
  62. },
  63. countVisibility: false,
  64. soundOn: false,
  65. correct: false,
  66. incorrect: false,
  67. correctCity: false,
  68. candidate: '',
  69. question: ''
  70. }
  71. }
  72.  
  73. ...
  74.  
  75. // getRandomQuestion = () => {
  76. // let randomN = Utilities.generateRandomNumber(this.array, 110);
  77. // return randomN;
  78. // }
  79.  
  80. getNewQuestion = () => {
  81. var randomN = getRandomNumber();
  82. var catArray = this.state.data.data.questions;
  83. Shuffle(catArray);
  84. this.setState({
  85. question: catArray[randomN]
  86. })
  87. this.setCatBgColor(catArray[randomN].category);
  88. }
  89.  
  90. setCatBgColor = (cat) => {
  91. switch (cat) {
  92. case "Banking & Finance":
  93. this.setState({
  94. catBgColor: 'n-0'
  95. })
  96. break;
  97.  
  98. case "Entrepreneurship":
  99. this.setState({
  100. catBgColor: 'n-1'
  101. })
  102. break;
  103.  
  104. case "Ideas, big & small":
  105. this.setState({
  106. catBgColor: 'n-2'
  107. })
  108. break;
  109.  
  110. case "Analytical thinking":
  111. this.setState({
  112. catBgColor: 'n-3'
  113. })
  114. break;
  115.  
  116. case "Emotional Intelligence":
  117. this.setState({
  118. catBgColor: 'n-4'
  119. })
  120. break;
  121. }
  122. }
  123.  
  124. setHeaderVisible = (val) => {
  125. this.setState({
  126. h1: val
  127. })
  128. }
  129.  
  130. setVisibility = (form, categories, grid, timeUp, city) => {
  131. this.setState({
  132. visibility: {
  133. form,
  134. categories,
  135. grid,
  136. timeUp,
  137. city
  138. }
  139. })
  140. }
  141.  
  142. componentDidMount() {
  143. let rN = getRandomNumber();
  144. axios.get('/get-questions')
  145. .then(r => {
  146. this.setState({
  147. data: r,
  148. question: r.data.questions[rN]
  149. })
  150. });
  151. this.setBgImg();
  152. }
  153.  
  154. render () {
  155. let currentState = this.state,
  156. form,
  157. cat,
  158. grid,
  159. timeUp,
  160. incorrect,
  161. correct,
  162. isH1Visible = currentState.h1 ? "hide" : "";
  163.  
  164. if(currentState.visibility.form) {
  165. form = <Form
  166. visibility = { currentState.visibility.form }
  167. setVisibility = { this.setVisibility }
  168. setCandidate = { this.setCandidate }
  169. />
  170. }
  171.  
  172. if(currentState.visibility.categories) {
  173. cat = <Categories
  174. visibility = { currentState.visibility.categories }
  175. setVisibility = { this.setVisibility }
  176. setHeaderVisible = { this.setHeaderVisible }
  177. current = { currentState.data }
  178. setBgColor = { this.setBgColor }
  179. setSoundOn = { this.setPlaySoundState }
  180. setCatBgColor = { this.setCatBgColor }
  181. question = { this.state.question } />
  182. }
  183.  
  184. if(currentState.visibility.grid) {
  185. grid = <Grid
  186. visibility = { currentState.visibility.grid }
  187. visibilityCustomCity = { currentState.visibility.city }
  188. current = { currentState.data }
  189. points = { currentState.points }
  190. catBgColor = { currentState.catBgColor }
  191. bgImg = { currentState.bgImg }
  192. question = { currentState.question }
  193. setFinalPlay = { this.setFinalPlay }
  194. setBgColor = { this.setBgColor }
  195. setCountVisibility = { this.setCountVisibility }
  196. setSoundOn = { this.setPlaySoundState }
  197. setIncorrectOn = { this.setIncorrectOn }
  198. setCorrectOn = { this.setCorrectOn }
  199. getNewQuestion = { this.getNewQuestion }
  200. addPoints = { this.addPoints }
  201. setCatBgColor = { this.setCatBgColor }
  202. setVisibility = { this.setVisibility }
  203. setCorrectCitySoundOn = { this.setCorrectCity }
  204. />
  205. }
  206.  
  207. if(currentState.visibility.timeUp) {
  208. timeUp = <TimeUp
  209. visibility = { currentState.visibility.timeUp }
  210. setVisibility = { this.setVisibility }
  211. setCountVisibility = { this.setCountVisibility }
  212. />
  213. }
  214.  
  215. if(currentState.finalPlay.correct) {
  216. correct = <Correct
  217. setBgImg = { this.setBgImg}
  218. setCandidate = { this.setCandidate}
  219. candidate = { this.state.candidate}
  220. setBgColor = { this.setBgColor}
  221. resetPoints = { this.resetPoints}
  222. setHeaderVisible = { this.setHeaderVisible}
  223. points = { currentState.points}
  224. setFinalPlay = { this.setFinalPlay}
  225. setVisibility = { this.setVisibility}
  226. setCorrectCitySoundOn = { this.setCorrectCity } />
  227. }
  228.  
  229. if(currentState.finalPlay.incorrect) {
  230. incorrect = <Incorrect
  231. setBgImg = { this.setBgImg}
  232. setCandidate = { this.setCandidate}
  233. candidate = { this.state.candidate}
  234. setBgColor = { this.setBgColor}
  235. resetPoints = { this.resetPoints}
  236. setHeaderVisible = { this.setHeaderVisible}
  237. points = { currentState.points}
  238. setFinalPlay = { this.setFinalPlay}
  239. setVisibility = { this.setVisibility}
  240. setCorrectCitySoundOn = { this.setCorrectCity } />
  241. }
  242.  
  243. return (
  244. <div className = { "wrapper " + currentState.bgColor}>
  245. <div className="wrapper-inner">
  246. <CustomSound soundOn = { this.state.soundOn} />
  247. <CorrectSound soundOn = { this.state.correct} />
  248. <IncorrectSound soundOn = { this.state.incorrect} />
  249. <CorrectCitySound soundOn = { this.state.correctCity} />
  250. <h1 className = { isH1Visible}><span>the big</span> reveal</h1>
  251. {form}
  252. {cat}
  253. {grid}
  254. {timeUp}
  255. {correct}
  256. {incorrect}
  257. </div>
  258. </div>
  259. )
  260. }
  261. }
  262.  
  263. export default App;
  264.  
  265. ReactDOM.render(<App />, document.getElementById('app'));
  266.  
  267. var cityArray = [];
  268.  
  269. function getRandomNumber() {
  270. let randomN = Utilities.generateRandomNumber(cityArray, 10);
  271. return randomN;
  272. }
  273.  
  274. getNewQuestion = () => {
  275. var randomN = getRandomNumber();
  276. var catArray = this.state.data.data.questions;
  277. Shuffle(catArray);
  278. this.setState({
  279. question: catArray[randomN]
  280. })
  281. this.setCatBgColor(catArray[randomN].category);
  282. }
Add Comment
Please, Sign In to add comment