Access the JSON Data from an API Challenge

Access the JSON Data from an API Challenge
0

#1

Tell us what’s happening:

Im on the Access the JSON Data from an API challenge and it seems to be working as expected with the output but is not passing the test. Has anybody else gotten this challenge to pass? Im not sure if it’s the way I did it or maybe if its bugged?

Your code so far


<script>
  document.addEventListener('DOMContentLoaded',function(){
    document.getElementById('getMessage').onclick=function(){
      req=new XMLHttpRequest();
      req.open("GET",'/json/cats.json',true);
      req.send();
      req.onload=function(){
        json=JSON.parse(req.responseText);
        document.getElementsByClassName('message')[0].innerHTML=JSON.stringify(json);
        // Add your code below this line
        console.log(json.find(item => item.id == 2).codeNames[1])
        
        // Add your code above this line
      };
    };
  });
</script>
<style>
  body {
    text-align: center;
    font-family: "Helvetica", sans-serif;
  }
  h1 {
    font-size: 2em;
    font-weight: bold;
  }
  .box {
    border-radius: 5px;
    background-color: #eee;
    padding: 20px 5px;
  }
  button {
    color: white;
    background-color: #4791d0;
    border-radius: 5px;
    border: 1px solid #4791d0;
    padding: 5px 10px 8px 10px;
  }
  button:hover {
    background-color: #0F5897;
    border: 1px solid #0F5897;
  }
</style>
<h1>Cat Photo Finder</h1> 
<p class="message box">
  The message will go here
</p>
<p>
  <button id="getMessage">
    Get Message
  </button>
</p>

It does say

You should use bracket and dot notation on the object (which is saved in the variable json) to access the value.

in the challenge so I tried a more round about way of doing it but it also didn’t pass the test.

let i = json.findIndex(item => item.id == 2);
console.log(json[i].codeNames[1]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/data-visualization/json-apis-and-ajax/access-the-json-data-from-an-api


#2

It looks like you’ve gone above and beyond the test’s expecatations by using the find method (good on you though!) you can pass this test by the following line:

console.log(json[2].codeNames[1])