Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const reducer = require('./reducer');
- function getTenMostPopulous(obj) {
- return Object.keys(obj).sort((a, b) => obj[a] - obj[b]).slice(-10).reverse();
- }
- function percent(a, b) {
- if(typeof b === 'object') {
- const total = Object.values(b).reduce((a,b) => a + b);
- return (a/total) * 100;
- }
- return (a/(a + b)) * 100;
- }
- function struct(data) {
- const finalData = {results: []};
- const percentFemale = reducer.percentageFemale(data);
- finalData.results[0] = {
- title: 'Percentage Female vs Male',
- value: percent(percentFemale.female/percentFemale.male),
- femaleCount: percentFemale.female,
- maleCount: percentFemale.male
- };
- const percentFirst = reducer.percentageFirst(data);
- finalData.results[1] = {
- title: 'Percentage First Names Starting with A-M vs N-Z',
- value: percent(percentFirst.AM/percentFirst.NZ),
- amCount: percentFirst.AM,
- nzCount: percentFirst.NZ
- };
- const percentLast = reducer.percentageLast(data);
- finalData.results[2] = {
- title: 'Percentage Last Names Starting with A-M vs N-Z',
- value: percent(percentLast.AM/percentLast.NZ),
- amCount: percentLast.AM,
- nzCount: percentLast.NZ
- };
- const percentStates = reducer.percentageStates(data);
- finalData.results[3] = {
- title: 'Percentage Female vs Male in Ten Most Populous States',
- states: getTenMostPopulous(percentStates.total).map((state) => {
- return {
- name: state,
- value: percent(percentStates.female[state], percentStates.male[state]),
- femaleCount: percentStates.female[state],
- maleCount: percentStates.male[state],
- }
- })
- };
- const percentAge = reducer.percentageAge(data);
- finalData.results[4] = {
- title: 'Percentage People In Each Age Range',
- ranges: function(obj) {
- for (const range in obj) {
- range: {
- value: percent(obj[range], obj),
- count: obj[range];
- }(percentAge);
- }
- }
- }
- return finalData;
- }
- const fakeData = {
- results = [
- {
- title: 'Percentage Female vs Male',
- value: '50%',
- femaleCount: 10,
- maleCount: 10
- },
- {
- title: 'Percentage First Names Starting with A-M vs N-Z',
- value: '75%',
- amCount: 3,
- nzCount: 1
- },
- {
- title: 'Percentage Last Names Starting with A-M vs N-Z',
- value: '75%',
- amCount: 3,
- nzCount: 1
- },
- {
- title: 'Percentage Female vs Male in Ten Most Populous States',
- states: [
- {
- name: 'New York',
- value: '35%',
- femaleCount: 12,
- maleCount: 15
- },
- {
- name: 'Montana',
- value: '35%',
- femaleCount: 12,
- maleCount: 15
- },
- {
- name: 'New Hampshire',
- value: '35%',
- femaleCount: 12,
- maleCount: 15
- },
- {
- name: 'California',
- value: '35%',
- femaleCount: 12,
- maleCount: 15
- },
- {
- name: 'Florida',
- value: '35%',
- femaleCount: 12,
- maleCount: 15
- },
- {
- name: 'Oregon',
- value: '35%',
- femaleCount: 12,
- maleCount: 15
- },
- {
- name: 'Alabama',
- value: '35%',
- femaleCount: 12,
- maleCount: 15
- },
- {
- name: 'Connecticut',
- value: '35%',
- femaleCount: 12,
- maleCount: 15
- },
- {
- name: 'New Mexico',
- value: '35%',
- femaleCount: 12,
- maleCount: 15
- },
- {
- name: 'New Jersey',
- value: '35%',
- femaleCount: 12,
- maleCount: 15
- },
- ]
- },
- {
- title: 'Percentage People In Each Age Range',
- ranges: {
- '0-20': {
- value: '10%',
- count: 5
- },
- '21-40': {
- value: '10%',
- count: 5
- },
- '41-60': {
- value: '10%',
- count: 5
- },
- '61-80': {
- value: '10%',
- count: 5
- },
- '81-100': {
- value: '10%',
- count: 5
- },
- '100+': {
- value: '10%',
- count: 5
- }
- }
- }
- ]
- }
- module.exports = {struct};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement