Skip to content

Instantly share code, notes, and snippets.

@dannycroft
Last active June 1, 2018 09:47
Show Gist options
  • Save dannycroft/54c3a5501c7d1e3aa5e155536642f363 to your computer and use it in GitHub Desktop.
Save dannycroft/54c3a5501c7d1e3aa5e155536642f363 to your computer and use it in GitHub Desktop.
Simple challenge to collect and parse users
/* -----------------------------------------------------------------------------
Complete the following tasks using only browser based APIs. Update
the supplied "getUsers()" function to achieve the following:
- Make a request to the API_URL (see below)
- Return a Promise
- Parse each of the returned results to match the example below:
[
{
name: 'Jane Doe',
age: 69,
email: 'jane.doe@example.com'
},{
...
}
]
Notes: Please return an array of objects. Each object should contain
the following: name, age and email. Running the assertions below will
help you return the correct results
----------------------------------------------------------------------------- */
const API_URL = 'https://randomuser.me/api/?results=10';
function getUsers() {}
/* ------------------ TEST ASSERTIONS - PLEASE DO NOT EDIT ------------------ */
try {
getUsers()
.then(users => {
console.assert(users.length === 10, 'expected user array to have a length of 10');
users.forEach(user => {
console.assert(user.hasOwnProperty('name'), `expected user to have a name`);
console.assert(user.hasOwnProperty('email'), `expected user to have an email`);
console.assert(user.hasOwnProperty('age'), `expected user to have an age`);
});
console.log(`Current users -> `, users);
})
.catch(err => console.error(`Test failure -> `, err));
} catch (err) {
console.error(`Test failure -> `, err);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment