Chuck Norris

JSON challenge

by Esther Mun

HTML

<body>
    <h1>Chuck Norris</h1>
    <p>
        In this challenge, you will be working with JSON data that includes real facts about Chuck Norris. In the JavaScript panel of the JSFiddle, add code starting from line 15. The variable "joke" contains a JSON string. you must check what this string contains, then parse it and display the joke in either an "alert()" or a "console.log".
    </p>
    <p>
        Remember that accessing an object inside an object can be done using the notation "data.parentObject.childObject".
    </p>
</body>

JavaScript

// Don't worry about what's going on here!
function getJoke(cb) {
	var req = new XMLHttpRequest();
	req.onreadystatechange = function() {
		if (req.readyState != 4 || (req.status !=200 && req.status != 304)) return;
        cb(req.responseText);
	}
	req.open("GET", "http://api.icndb.com/jokes/random?exclude=[nerdy,explicit]");req.send();
}
// --------------------------------------

getJoke(function(res) {
    var joke = res; 
    
    // your code here - what is 'res'?
    //parse JSON data and display it
    var chuckData = JSON.parse(res);
    console.log(chuckData.value.joke);
    
});