Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Asynchronous Programming with JavaScript Asynchronous JavaScript with Callbacks Implement a Callback

Katarzyna Russek
Katarzyna Russek
6,086 Points

What if there are more people with the exact same name as the astronaut?

I see a note on the page:

Michael Barratt Results unavailable for Michael Barratt

Michael Barratt may refer to:

Michael Barratt (1928–2022), British television presenter Michael Barratt (astronaut), American astronaut Shakin' Stevens, Welsh singer

2 Answers

Steven Parker
Steven Parker
231,272 Points

This would make the project a good bit more complex, but the basic idea would be to check the initial reply to see if you get a "disambiguation" response type. When that happens, repeat the initial request but add the string " (astronaut)" onto the name.

Steven Parker
Steven Parker
231,272 Points

Ulrich Doerr — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!

Ulrich Doerr
Ulrich Doerr
2,491 Points

I tried:

function getAstronautSummary(url, callback) {
  const xhr = new XMLHttpRequest();
  xhr.open('GET', url);
  xhr.onload = () => {
    if(xhr.status === 200) {
      let dt = JSON.parse(xhr.responseText);
      if (dt.type="disambiguation") {
        return getJSON(url+ " (astronaut)", callback);
      } 
      return callback(dt);
    }
  };
  xhr.send();
}

and modified

btn.addEventListener('click', (event) => { 
  getJSON(astrosUrl, (json) => {
    json.people.map( person => {
      getAstronautSummary(wikiUrl + person.name, generateHTML);
    });
  });
  event.target.remove();
});

Worked just fine ...

... except all sections prior to Michael Barratt are lost.

Could'nt figure out, why.

Ulrich Doerr
Ulrich Doerr
2,491 Points

slightly embarrassed: it has to be

      if (dt.type === "disambiguation") {
          ...
Steven Parker
Steven Parker
231,272 Points

… or just ==. Either kind of comparison instead of an assignment. :see_no_evil: