freeCodeCamp/seed/challenges/json-apis-and-ajax.json

402 lines
15 KiB
JSON
Raw Normal View History

{
"name": "JSON APIs and Ajax",
"order": 0.0065,
"challenges": [
{
"id": "bb000000000000000000001",
"title": "Trigger on click Events with jQuery",
"difficulty": 3.19,
"description": [
"With jQuery we are able to get data from APIs via Ajax.",
"This data normally comes in the form of JSON.",
"Let's get the <code>Get Message</code> button to set the text of a div.",
"We will later use this to display the result of out API request.",
"<code>$(\"#getMessage\").on(\"click\", function(){</code>",
"<code>&thinsp;&thinsp;$(\".message\").html(\"Here is the message\");</code>",
"<code>});</code>"
],
"tests": [
"assert(editor.match(/\\$\\s*?\\(\\s*?(?:'|\")\\#getMessage(?:'|\")\\s*?\\)\\s*?\\.on\\s*?\\(\\s*?(?:'|\")click(?:'|\")\\s*?\\,\\s*?function\\s*?\\(\\s*?\\)\\s*?\\{/gi), 'You should have bound the click event to the getMessage button.')",
"assert(editor.match(/\\$\\s*?\\(\\s*?(?:'|\")\\.message(?:'|\")\\s*?\\)\\s*?\\.html\\s*?\\(\\s*?(?:'|\")Here\\sis\\sthe\\smessage(?:'|\")\\s*?\\);/gi), 'You should set te value of the #message box to be the message given in the description.')",
"assert(editor.match(/\\n*?\\s*?\\}\\n*?\\s*?\\);/gi) && editor.match(/\\n*?\\s*?\\}\\);/gi).length >= 2, 'Make sure that you close off all of your functions.')"
],
"challengeSeed": [
"fccss",
" $(document).ready(function() {",
" ",
" });",
"fcces",
"",
"<!-- You shouldn't need to modify code below this line -->",
"",
"<div class=\"container-fluid\">",
" <div class = \"row text-center\">",
" <h2>Cat Photo Finder</h2>",
" </div>",
" <br/>",
" <div class = \"row text-center\">",
" <div class = \"col-xs-12 well Message\">",
" The message will go here",
" </div>",
" </div>",
" <br/>",
" <div class = \"row text-center\">",
" <div class = \"col-xs-12\">",
" <button id = \"getMessage\" class = \"btn btn-primary\">",
" Get Message",
" </button>",
" </div>",
" </div>",
"</div>"
],
"challengeType": 0,
"type": "waypoint"
},
{
"id": "bb000000000000000000002",
2015-09-30 22:01:56 +00:00
"title": "Creating HTML from Data from an AJAX request Using jQuery",
"difficulty": 3.20,
"description": [
"",
"We are now going to request data from an external source. (a file on FCC for the purposes of this exercise) The request will load in the data an run the code in the function we provide the data to which is known as the callback.",
"<code>$(\"#getMessage\").on(\"click\", function() {",
2015-09-30 22:01:56 +00:00
"&thinsp;&thinsp;$.getJSON(\"/json/cats.json?callback=\", function( json ) {",
"&thinsp;&thinsp;&thinsp;&thinsp;//Code to run when request is complete",
"&thinsp;&thinsp;&thinsp;&thinsp;$(\".message\").html(JSON.stringify(json))",
"&thinsp;&thinsp;});",
"});</code>",
"Let's make it so that the data sent from the request is appended to the .message div.",
""
],
"tests": [
"assert(editor.match(/\\$\\s*?\\(\\s*?(\\\"|\\')\\#getMessage(\\\"|\\')\\s*?\\)\\s*?\\.\\s*?on\\s*?\\(\\s*?(\\\"|\\')click(\\\"|\\')\\s*?\\,\\s*?function\\s*?\\(\\s*?\\)\\s*?\\{/gi), 'You should have a click handler on the getMessage button to trigger the AJAX request.')",
"assert(editor.match(/\\s*?\\}\\s*?\\)\\s*?\\;/gi), 'You should have at least on closing set of brackets and parenthesis.')",
"assert(editor.match(/\\s*?\\}\\s*?\\)\\s*?\\;/gi) && editor.match(/\\,\\s*?function\\s*?\\(\\s*?\\w*?\\s*?\\)\\s*?\\{/gi) && editor.match(/\\s*?\\}\\s*?\\)\\s*?\\;/gi).length === editor.match(/\\s*?function\\s*?\\(\\s*?\\w*?\\s*?\\)\\s*?\\{/gi).length, 'Each callback function should have a closing set of brackets and parenthesis.')",
"assert(editor.match(/\\$\\s*?\\.\\s*?getJSON\\s*?\\(\\s*?\"\\\/json\\\/cats\\.json\\?callback\\=\"\\s*?\\,\\s*?function\\s*?\\(\\s*?json\\s*?\\)\\s*?\\{/gi), 'You should be making use of the getJSON method given in the description to load data from the json file.')",
"assert(editor.match(/\\$\\s*?\\(\\s*?\\\"\\.message\\\"\\s*?\\)\\s*?\\.\\s*?html\\s*?\\(\\s*?JSON\\s*?\\.\\s*?stringify\\s*?\\(\\s*?json\\s*?\\)\\s*?\\)/gi), 'Don\\'t forget to make the <code>.html</code> change the contents of the message box so that it contains the result of the getJSON.')"
],
"challengeSeed": [
"fccss",
" $(document).ready(function() {",
" ",
" $(\"#getMessage\").on(\"click\", function(){",
" $(\".message\").html(\"Make the result of the getJSON request appear here\")",
" });",
" ",
" });",
"fcces",
"",
"<!-- You shouldn't need to modify code below this line -->",
"",
"<div class=\"container-fluid\">",
" <div class = \"row text-center\">",
" <h2>Cat Photo Finder</h2>",
" </div>",
" <br/>",
" <div class = \"row text-center\">",
" <div class = \"col-xs-12 well Message\">",
" The message will go here",
" </div>",
" </div>",
" <br/>",
" <div class = \"row text-center\">",
" <div class = \"col-xs-12\">",
" <button id = \"getMessage\" class = \"btn btn-primary\">",
" Get Message",
" </button>",
" </div>",
" </div>",
"</div>"
],
"challengeType": 0,
"type": "waypoint"
2015-09-30 22:01:56 +00:00
},
{
"id": "bb000000000000000000003",
"title": "Convert JSON data to HTML",
2015-09-30 22:01:56 +00:00
"difficulty": 3.21,
"description": [
"",
"Now that we have the data let's re-arrange it so that it can be displayed in a user friendly way.",
2015-09-30 22:01:56 +00:00
"",
"<code>",
"&thinsp;&thinsp;json.map(function(val){",
"&thinsp;&thinsp;&thinsp;&thinsp;",
"&thinsp;&thinsp;&thinsp;&thinsp;html = html + \"&lt;div class = 'cat'&gt;\"",
"&thinsp;&thinsp;&thinsp;&thinsp;",
"&thinsp;&thinsp;&thinsp;&thinsp;for(var key in val){",
"&thinsp;&thinsp;&thinsp;&thinsp;&thinsp;&thinsp;html = html + '&lt;div class = \"' + key + '\"&gt;' + val[key] + '&lt;/div&gt;';",
"&thinsp;&thinsp;}",
"&thinsp;&thinsp;&thinsp;&thinsp;",
"&thinsp;&thinsp;&thinsp;&thinsp;html = html + \"&lt;/div&gt;&lt;br/&gt;\"",
"&thinsp;&thinsp;&thinsp;&thinsp;",
"&thinsp;&thinsp;});",
"</code>",
""
],
"tests": [
"assert(/json\\.map/gi, 'The message box should have something in it.')"
2015-10-06 20:47:29 +00:00
],
2015-09-30 22:01:56 +00:00
"challengeSeed": [
"fccss",
" $(document).ready(function() {",
" ",
" $(\"#getMessage\").on(\"click\", function() {",
" $.getJSON(\"/json/cats.json?callback=\", function( json ) {",
" ",
" var html = \"\";",
" ",
" //Add you code to modify the data here. It should add it to the html sting for use in the view",
" ",
" //Don't modify above here",
2015-09-30 22:01:56 +00:00
" ",
" ",
" ",
" //Don't modify below here",
2015-09-30 22:01:56 +00:00
" ",
" $(\".message\").html(html);",
" ",
" });",
" });",
" ",
" });",
"fcces",
"",
"<!-- You shouldn't need to modify code below this line -->",
"",
"<div class=\"container-fluid\">",
" <div class = \"row text-center\">",
" <h2>Cat Photo Finder</h2>",
" </div>",
" <br/>",
" <div class = \"row text-center\">",
" <div class = \"col-xs-12 well Message\">",
" The message will go here"," </div>",
" </div>",
" <br/>",
" <div class = \"row text-center\">",
" <div class = \"col-xs-12\">",
" <button id = \"getMessage\" class = \"btn btn-primary\">",
" Get Message",
" </button>",
" </div>",
" </div>",
"</div>"
],
"challengeType": 0,
"type": "waypoint"
},
{
"id": "bb000000000000000000004",
"title": "render those images!",
"difficulty": 3.22,
"description": [
"",
"instead of just placing everything in a div we should check if the value is an image.",
"If it is an image we should use it as an ima tag instead so that the image is rendered.",
"<code>",
"if(key === \"imageLink\"){",
"html = html + '&lt;img class = \"' + key + '\"src = \"' + val[key] + '\"&gt;';",
"}",
"else{",
" html = html + '&lt;div class = \"' + key + '\"&gt;' + val[key] + '&lt;/div&gt;';",
"}",
"</code>",
""
],
"tests": [
"assert(editor.match(/imageLink/gi), 'You should have accessed the imageLink of each cat object.')"
],
"challengeSeed": [
"fccss",
"$(document).ready(function() {",
" ",
" $(\"#getMessage\").on(\"click\", function() {",
" $.getJSON(\"/json/cats.json?callback=\", function( json ) {",
" ",
" var html = \"\";",
" ",
" //Add you code to modify the data here. It should add it to the html sting for use in the view",
" ",
" json.map(function(val){",
"",
" html = html + \"<div class = 'cat'>\"",
"",
"",
"",
" for(var key in val){",
"",
" //Don't modify above here",
" ",
" ",
" ",
" //Don't modify below here",
"",
" }",
" ",
" html = html + \"</div><br/>\"",
"",
" });",
" ",
" //Don't modify above here",
" ",
" $(\".message\").html(html);",
" ",
" });",
" });",
" ",
" });",
"fcces",
"",
"<!-- You shouldn't need to modify code below this line -->",
"",
"<div class=\"container-fluid\">",
" <div class = \"row text-center\">",
" <h2>Cat Photo Finder</h2>",
" </div>",
" <br/>",
" <div class = \"row text-center\">",
" <div class = \"col-xs-12 well Message\">",
" The message will go here",
" </div>",
" </div>",
" <br/>",
" <div class = \"row text-center\">",
" <div class = \"col-xs-12\">",
" <button id = \"getMessage\" class = \"btn btn-primary\">",
" Get Message",
" </button>",
" </div>",
" </div>",
"</div>",
""
],
"challengeType": 0,
"type": "waypoint"
},
{
"id": "bb000000000000000000005",
"title": "Pre-filtering the JSON",
"difficulty": 3.22,
"description": [
"",
"This means we should never hit API limits and it will make the process more efficient.",
"Let's try pre-filtering the json before we map it.",
"We can use the pre-made filter method like this to remove the cat with the id of 1.",
"<code>",
"json = json.filter(function(val){",
" return(val.id !== 1);",
"});",
"</code>",
""
],
"tests": [
"assert(editor.match(/filter/gi), 'You should be making use of the .filter method.')"
],
"challengeSeed": [
"fccss",
"$(document).ready(function() {",
" ",
" $(\"#getMessage\").on(\"click\", function() {",
" $.getJSON(\"/json/cats.json?callback=\", function( json ) {",
" ",
" var html = \"\";",
" ",
" //Add you code to modify the data here. It should add it to the html sting for use in the view",
" ",
" json.map(function(val){",
"",
" val = \"<img src = '\" + val.imageLink + \"'/>\"",
"",
" html = html + \"<div class = 'cat'>\"",
"",
" //Don't modify above here",
" ",
" ",
" ",
" //Don't modify below here",
"",
" for(var key in val){",
"",
" html = html + '<div class = \"' + key + '\">' + val[key] + '</div>';",
"",
" }",
"",
" ",
" html = html + \"</div><br/>\"",
"",
" });",
" ",
" //Don't modify above here",
" ",
" $(\".message\").html(html);",
" ",
" });",
" });",
" ",
" });",
"fcces",
"",
"<!-- You shouldn't need to modify code below this line -->",
"",
"<div class=\"container-fluid\">",
" <div class = \"row text-center\">",
" <h2>Cat Photo Finder</h2>",
" </div>",
" <br/>",
" <div class = \"row text-center\">",
" <div class = \"col-xs-12 well Message\">",
" The message will go here",
" </div>",
" </div>",
" <br/>",
" <div class = \"row text-center\">",
" <div class = \"col-xs-12\">",
" <button id = \"getMessage\" class = \"btn btn-primary\">",
" Get Message",
" </button>",
" </div>",
" </div>",
"</div>",
""
],
"challengeType": 0,
"type": "waypoint"
},
{
"id": "bb000000000000000000006",
"title": "Getting Geo-location data for use in APIs",
"difficulty": 3.19,
"description": [
"",
"We can access the users current location by using the built in navigator in the browser.",
"The navigator will get the users current longitude and latitude with a decent level of accuracy.",
"<code>",
"if (navigator.geolocation) {",
"&thinsp;&thinsp;navigator.geolocation.getCurrentPosition(function(position){",
"&thinsp;&thinsp;&thinsp;&thinsp;// Do something in here with the coordinates!",
"&thinsp;&thinsp;&thinsp;&thinsp;",
"&thinsp;&thinsp;&thinsp;&thinsp;$(\"#data\").html(\"latitiude\" + position.coords.latitude + \"longitude\" + position.coords.longitude);",
"&thinsp;&thinsp;&thinsp;&thinsp;",
"&thinsp;&thinsp;});",
"}",
"</code>"
],
"tests": [
"assert(editor.match(/navigator\\.geolocation\\.getCurrentPosition/gi), 'you should make use of the <code>navigator.geolocation</code> to access the users current location.')"
],
"challengeSeed": [
"fccss",
"",
"fcces",
"<div id = \"data\">",
" <h4>You are here!</h4>",
" ",
"</div>"
],
"challengeType": 0,
"type": "waypoint"
}
]
}