{"id":18572735,"url":"https://github.com/truecodersio/js_fetch_exercise_2","last_synced_at":"2025-10-13T14:34:10.022Z","repository":{"id":234302872,"uuid":"788615134","full_name":"truecodersio/JS_Fetch_Exercise_2","owner":"truecodersio","description":"Uses the randomuser.me API to retrieve data about random users(s)","archived":false,"fork":false,"pushed_at":"2024-04-18T18:56:24.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-17T14:31:53.019Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/truecodersio.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-04-18T18:56:14.000Z","updated_at":"2024-04-18T18:56:14.000Z","dependencies_parsed_at":"2024-04-18T19:57:52.881Z","dependency_job_id":"ae763eff-35cd-4c12-a2c0-f88164c757eb","html_url":"https://github.com/truecodersio/JS_Fetch_Exercise_2","commit_stats":null,"previous_names":["truecodersio/js_fetch_exercise_2"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/truecodersio/JS_Fetch_Exercise_2","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/truecodersio%2FJS_Fetch_Exercise_2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/truecodersio%2FJS_Fetch_Exercise_2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/truecodersio%2FJS_Fetch_Exercise_2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/truecodersio%2FJS_Fetch_Exercise_2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/truecodersio","download_url":"https://codeload.github.com/truecodersio/JS_Fetch_Exercise_2/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/truecodersio%2FJS_Fetch_Exercise_2/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279015740,"owners_count":26085748,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-13T02:00:06.723Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T23:07:05.397Z","updated_at":"2025-10-13T14:34:10.007Z","avatar_url":"https://github.com/truecodersio.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fetch and Display Users Exercise\n\n## API Information\nWe'll be using [this random user generator API](https://randomuser.me/documentation) to get information about user(s) and display that information on the webpage.\n\nThe API can be accessed by making a GET fetch request to this url: https://randomuser.me/api/\n\nTo gain a better understanding about the API and the data it returns, I recommend going to that URL in your web browser and looking at the data it gives you.\n\nThis is the same process that we need to recreate by using the `fetch()` method in our JavaScript code.\n\n## Coding Steps\n\n### Setup\n1. Make a new folder inside of `/repos` and open it with Visual Studio Code\n1. Make the usual `index.html` file\n1. Be sure to attach a link to a `script.js` file and defer its execution\n1. Optionally make a `style.css` file and link to it inside of your html file\n1. Feel free to add some HTML and CSS to your webpage to make it your own. Maybe create an `h1` tag, and/or `p` tags... Have fun with it\n\n### JavaScript (Within script.js)\n1. Create a function called `getUserData`. It doesn't require any parameters yet (we will refactor this function in a bit)\n1. Use the `fetch()` method to send a request to this URL: `https://randomuser.me/api/`\n1. Consume the promise that it returns by chaining a `.then()` method onto your `fetch`.\n1. Provide a callback function as a parameter to the `.then()` method. The callback function should have a parameter (we'll name it `response`). The callback function's body should parse the response object's JSON data by invoking `.json()` and return the result.\n1. Handle the next promise by again chaining a `.then()` method onto the previous `.then()` method.\n1. Again, provide a callback function which takes one parameter (we'll call it data). This time the callback function's body should simply log out the `data` parameter.\n1. Handle any potential errors by chaining a `.catch()` method onto the previous `.then()` method.\n1. Provide the `.catch()` with a callback function which takes one parameter (we'll call it err). The callback function's body should `console.error()` the `err` parameter.\n\n### Checking our work so far\n1. Above or below this function you just made, simply invoke the function `getUserData()`.\n1. Run and test your code. Open the browser console to see if the random user data shows up in your console. Work through any errors you may be getting.\n\n### Extract and Display the data\nNow that you're able to get a user's data, extract just the pieces of information that you want to display\n\n1. Create a new function called `displayUser`. This function will take a parameter (we'll call it `userData`).\n1. Create temporary variables for the user's name, city, country, email, and large picture. Each variable will need to be assigned the appropriate value from the `userData` object. For example, the name will need to be a concatenation of `userData.name.first` and `userData.name.last`. (You can include the title if you'd like, as well)\n\nCreate HTML elements to contain the information and add them to the DOM. You'll need to use DOM methods and properties like `document.createElement()`, `.append()`, `.textContent`, etc...\n\n1. Create a div with a class of `userContainer`\n1. Create an img with a `src` property that points to the large picture variable you created earlier, and an `alt ` property that uses the name variable you created earlier.\n1. Create separate paragraph tags for the user's name, city, country, and email. Be sure to set each of their `textContent` properties to the appropriate variables you made earlier.\n1. Append the img and paragraphs to the `userContainer` div\n1. Append the div onto the DOM wherever you'd like (Select an element first, or simply attach onto the `document.body`)\n\nLastly, you'll need to invoke this `displayUser` function at the right time\n\n1. You can only show a user's data on the screen once you *have* a user's data. So, back in the `getUserData` function, inside the callback function of the **second** `.then()` method, invoke the function: `displayUser(data.results[0]);`\n\nNow all the pieces of our code should be tied together, and hopefully, you'll see the user's data on your webpage.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftruecodersio%2Fjs_fetch_exercise_2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftruecodersio%2Fjs_fetch_exercise_2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftruecodersio%2Fjs_fetch_exercise_2/lists"}