Question
/******************************************************************************* * Problem 07: getUserStats() * * Write function to get user statistics for iNaturalist users in the data results * Array. Each user has
/*******************************************************************************
* Problem 07: getUserStats()
*
* Write function to get user statistics for iNaturalist users in the data results
* Array. Each user has count information, for example:
*
* {
* user: {
* id: 216168,
* login: 'psweet',
* spam: false,
* suspended: false,
* created_at: '2016-04-13T21:54:34+00:00',
* login_autocomplete: 'psweet',
* login_exact: 'psweet',
* name: '',
* name_autocomplete: '',
* orcid: null,
* icon: 'https://static.inaturalist.org/attachments/users/icons/216168/thumb.jpg?1478731222',
* observations_count: 19096,
* identifications_count: 173355,
* journal_posts_count: 8,
* activity_count: 192459,
* species_count: 4960,
* universal_search_rank: 19096,
* roles: ['curator'],
* site_id: 1,
* icon_url: 'https://static.inaturalist.org/attachments/users/icons/216168/medium.jpg?1478731222'
* }
* }
*
* In the above example data, the user has the following counts that we are
* interested in collecting:
*
* observations_count: 19096, // number of observations
* identifications_count: 173355, // number of identifications
* species_count: 4960, // number of species
*
* Your function should loop through all observation Objects and get the `user`
* property. Using the `user`, get the observations_count, identifications_count,
* and species_count, and use them to create totals and a final average. Your
* function should return an Object with these stats, which looks like this:
*
* {
* count: 10, // the total number of users in this sample
* totals: {
* observations: 1234, // the total observations for all users in this sample
* identifications: 1234, // the total identifications for all users in this sample
* species: 1234 // the total species for all users in this sample
* },
* averages: {
* observations: 12, // the average observations for all users in this sample
* identifications: 12, // the average identifications for all users in this sample
* species: 12 // the average species for all users in this sample
* }
* }
******************************************************************************/
function getUserStats(data) {
// TODO
}
const data = require('./data');
const result0 = data.results[0];
const result1 = data.results[1];
const { getUserStats } = require('./observations');
describe('Problem 07 - getUserStats() function', function () {
let sample, samples, sampleData;
beforeEach(() => {
sample = Object.assign({}, result0);
samples = [sample];
sampleData = { results: samples };
});
test('should return an Object with the right properties', function () {
let result = getUserStats(sampleData);
expect(typeof result === 'object').toBe(true);
expect(typeof result.count === 'number').toBe(true);
expect(typeof result.totals === 'object').toBe(true);
expect(typeof result.totals.observations === 'number').toBe(true);
expect(typeof result.totals.identifications === 'number').toBe(true);
expect(typeof result.totals.species === 'number').toBe(true);
expect(typeof result.averages === 'object').toBe(true);
expect(typeof result.averages.observations === 'number').toBe(true);
expect(typeof result.averages.identifications === 'number').toBe(true);
expect(typeof result.averages.species === 'number').toBe(true);
});
test('should return an Object with correct count properties', function () {
let result = getUserStats(sampleData);
expect(result.count).toBe(samples.length);
samples = [sample, sample, sample];
let result2 = getUserStats({ results: samples });
expect(result2.count).toBe(samples.length);
});
test('should return an Object with correct totals', function () {
samples = [sample, sample, sample];
let result = getUserStats({ results: samples });
expect(result.count).toBe(samples.length);
expect(result.totals.observations).toBe(96);
expect(result.totals.identifications).toBe(351);
expect(result.totals.species).toBe(66);
});
test('should return an Object with correct averages', function () {
samples = [result0, result1];
let result = getUserStats({ results: samples });
expect(result.count).toBe(samples.length);
expect(result.averages.observations).toBe(76.5);
expect(result.averages.identifications).toBe(58.5);
expect(result.averages.species).toBe(55);
});
test('real-data should produce the expected stats Object', function () {
let result = getUserStats(data);
expect(result).toEqual({
averages: { identifications: 11.7, observations: 48.6, species: 36.3 },
count: 10,
totals: { identifications: 117, observations: 486, species: 363 }
});
});
});
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started