Advertisement
Guest User

Untitled

a guest
May 24th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. const reducer = require('./reducer');
  2.  
  3. function getTenMostPopulous(obj) {
  4. return Object.keys(obj).sort((a, b) => obj[a] - obj[b]).slice(-10).reverse();
  5. }
  6.  
  7. function percent(a, b) {
  8. if(typeof b === 'object') {
  9. const total = Object.values(b).reduce((a,b) => a + b);
  10. return (a/total) * 100;
  11. }
  12. return (a/(a + b)) * 100;
  13. }
  14.  
  15. function struct(data) {
  16. const finalData = {results: []};
  17.  
  18. const percentFemale = reducer.percentageFemale(data);
  19. finalData.results[0] = {
  20. title: 'Percentage Female vs Male',
  21. value: percent(percentFemale.female/percentFemale.male),
  22. femaleCount: percentFemale.female,
  23. maleCount: percentFemale.male
  24. };
  25.  
  26. const percentFirst = reducer.percentageFirst(data);
  27. finalData.results[1] = {
  28. title: 'Percentage First Names Starting with A-M vs N-Z',
  29. value: percent(percentFirst.AM/percentFirst.NZ),
  30. amCount: percentFirst.AM,
  31. nzCount: percentFirst.NZ
  32. };
  33.  
  34. const percentLast = reducer.percentageLast(data);
  35. finalData.results[2] = {
  36. title: 'Percentage Last Names Starting with A-M vs N-Z',
  37. value: percent(percentLast.AM/percentLast.NZ),
  38. amCount: percentLast.AM,
  39. nzCount: percentLast.NZ
  40. };
  41.  
  42. const percentStates = reducer.percentageStates(data);
  43. finalData.results[3] = {
  44. title: 'Percentage Female vs Male in Ten Most Populous States',
  45. states: getTenMostPopulous(percentStates.total).map((state) => {
  46. return {
  47. name: state,
  48. value: percent(percentStates.female[state], percentStates.male[state]),
  49. femaleCount: percentStates.female[state],
  50. maleCount: percentStates.male[state],
  51. }
  52. })
  53. };
  54.  
  55. const percentAge = reducer.percentageAge(data);
  56. finalData.results[4] = {
  57. title: 'Percentage People In Each Age Range',
  58. ranges: function(obj) {
  59. for (const range in obj) {
  60. range: {
  61. value: percent(obj[range], obj),
  62. count: obj[range];
  63. }(percentAge);
  64. }
  65. }
  66. }
  67.  
  68. return finalData;
  69. }
  70.  
  71.  
  72. const fakeData = {
  73. results = [
  74. {
  75. title: 'Percentage Female vs Male',
  76. value: '50%',
  77. femaleCount: 10,
  78. maleCount: 10
  79. },
  80. {
  81. title: 'Percentage First Names Starting with A-M vs N-Z',
  82. value: '75%',
  83. amCount: 3,
  84. nzCount: 1
  85. },
  86. {
  87. title: 'Percentage Last Names Starting with A-M vs N-Z',
  88. value: '75%',
  89. amCount: 3,
  90. nzCount: 1
  91. },
  92. {
  93. title: 'Percentage Female vs Male in Ten Most Populous States',
  94. states: [
  95. {
  96. name: 'New York',
  97. value: '35%',
  98. femaleCount: 12,
  99. maleCount: 15
  100. },
  101. {
  102. name: 'Montana',
  103. value: '35%',
  104. femaleCount: 12,
  105. maleCount: 15
  106. },
  107. {
  108. name: 'New Hampshire',
  109. value: '35%',
  110. femaleCount: 12,
  111. maleCount: 15
  112. },
  113. {
  114. name: 'California',
  115. value: '35%',
  116. femaleCount: 12,
  117. maleCount: 15
  118. },
  119. {
  120. name: 'Florida',
  121. value: '35%',
  122. femaleCount: 12,
  123. maleCount: 15
  124. },
  125. {
  126. name: 'Oregon',
  127. value: '35%',
  128. femaleCount: 12,
  129. maleCount: 15
  130. },
  131. {
  132. name: 'Alabama',
  133. value: '35%',
  134. femaleCount: 12,
  135. maleCount: 15
  136. },
  137. {
  138. name: 'Connecticut',
  139. value: '35%',
  140. femaleCount: 12,
  141. maleCount: 15
  142. },
  143. {
  144. name: 'New Mexico',
  145. value: '35%',
  146. femaleCount: 12,
  147. maleCount: 15
  148. },
  149. {
  150. name: 'New Jersey',
  151. value: '35%',
  152. femaleCount: 12,
  153. maleCount: 15
  154. },
  155. ]
  156. },
  157. {
  158. title: 'Percentage People In Each Age Range',
  159. ranges: {
  160. '0-20': {
  161. value: '10%',
  162. count: 5
  163. },
  164. '21-40': {
  165. value: '10%',
  166. count: 5
  167. },
  168. '41-60': {
  169. value: '10%',
  170. count: 5
  171. },
  172. '61-80': {
  173. value: '10%',
  174. count: 5
  175. },
  176. '81-100': {
  177. value: '10%',
  178. count: 5
  179. },
  180. '100+': {
  181. value: '10%',
  182. count: 5
  183. }
  184. }
  185. }
  186. ]
  187. }
  188.  
  189. module.exports = {struct};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement