Add Data Vis seed
This commit is contained in:
committed by
Mrugesh Mohapatra
parent
e29b79d24e
commit
807338faa7
@ -0,0 +1,190 @@
|
||||
{
|
||||
"name": "Data Visualization Projects",
|
||||
"order": 3,
|
||||
"time": "150 hours",
|
||||
"helpRoom": "Help",
|
||||
"challenges": [
|
||||
{
|
||||
"id": "bd7168d8c242eddfaeb5bd13",
|
||||
"title": "Visualize Data with a Bar Chart",
|
||||
"description": [
|
||||
"<strong>Objective:</strong> Build a <a href='https://codepen.io' target='_blank'>CodePen.io</a> app that is functionally similar to this: <a href='https://codepen.io/freeCodeCamp/full/GrZVaM' target='_blank'>https://codepen.io/freeCodeCamp/full/GrZVaM</a>.",
|
||||
"Fulfill the below <a href='https://en.wikipedia.org/wiki/User_story' target='_blank'>user stories</a> and get all of the tests to pass. Give it your own personal style.",
|
||||
"You can use HTML, JavaScript, CSS, and the D3 svg-based visualization library. The tests require axes to be generated using the D3 axis property, which automatically generates ticks along the axis. These ticks are required for passing the D3 tests because their positions are used to determine alignment of graphed elements. You will find information about generating axes at <a href='https://github.com/d3/d3/blob/master/API.md#axes-d3-axis' target='_blank'>https://github.com/d3/d3/blob/master/API.md#axes-d3-axis</a>. Required (non-virtual) DOM elements are queried on the moment of each test. If you use a frontend framework (like Vue for example), the test results may be inaccurate for dynamic content. We hope to accommodate them eventually, but these frameworks are not currently supported for D3 projects.",
|
||||
"<strong>User Story #1:</strong> My chart should have a title with a corresponding <code>id=\"title\"</code>.",
|
||||
"<strong>User Story #2:</strong> My chart should have a <code>g</code> element x-axis with a corresponding <code>id=\"x-axis\"</code>.",
|
||||
"<strong>User Story #3:</strong> My chart should have a <code>g</code> element y-axis with a corresponding <code>id=\"y-axis\"</code>.",
|
||||
"<strong>User Story #4:</strong> Both axes should contain multiple tick labels, each with the corresponding <code>class=\"tick\"</code>.",
|
||||
"<strong>User Story #5:</strong> My chart should have a <code>rect</code> element for each data point with a corresponding <code>class=\"bar\"</code> displaying the data.",
|
||||
"<strong>User Story #6:</strong> Each bar should have the properties <code>data-date</code> and <code>data-gdp</code> containing date and GDP values.",
|
||||
"<strong>User Story #7:</strong> The bar elements' <code>data-date</code> properties should match the order of the provided data.",
|
||||
"<strong>User Story #8:</strong> The bar elements' <code>data-gdp</code> properties should match the order of the provided data.",
|
||||
"<strong>User Story #9:</strong> Each bar element's height should accurately represent the data's corresponding GDP.",
|
||||
"<strong>User Story #10:</strong> The <code>data-date</code> attribute and its corresponding bar element should align with the corresponding value on the x-axis.",
|
||||
"<strong>User Story #11:</strong> The <code>data-gdp</code> attribute and its corresponding bar element should align with the corresponding value on the y-axis.",
|
||||
"<strong>User Story #12:</strong> I can mouse over an area and see a tooltip with a corresponding <code>id=\"tooltip\"</code> which displays more information about the area.",
|
||||
"<strong>User Story #13:</strong> My tooltip should have a <code>data-date</code> property that corresponds to the <code>data-date</code> of the active area.",
|
||||
"Here is the dataset you will need to complete this project: <code>https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json</code>",
|
||||
"You can build your project by forking <a href='https://codepen.io/freeCodeCamp/pen/MJjpwO' target='_blank'>this CodePen pen</a>. Or you can use this CDN link to run the tests in any environment you like: <code>https://gitcdn.link/repo/freeCodeCamp/testable-projects-fcc/master/build/bundle.js</code>.",
|
||||
"Once you're done, submit the URL to your working project with all its tests passing.",
|
||||
"Remember to use the <a href='https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514' target='_blank'>Read-Search-Ask</a> method if you get stuck."
|
||||
],
|
||||
"challengeSeed": [],
|
||||
"tests": [],
|
||||
"isRequired": true,
|
||||
"releasedOn": "January 1, 2016",
|
||||
"type": "zipline",
|
||||
"challengeType": 3,
|
||||
"translations": {
|
||||
"es": {
|
||||
"title": "Visualiza datos utilizando un gráfico de barras"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "bd7178d8c242eddfaeb5bd13",
|
||||
"title": "Visualize Data with a Scatterplot Graph",
|
||||
"description": [
|
||||
"<strong>Objective:</strong> Build a <a href='https://codepen.io' target='_blank'>CodePen.io</a> app that is functionally similar to this: <a href='https://codepen.io/freeCodeCamp/full/bgpXyK' target='_blank'>https://codepen.io/freeCodeCamp/full/bgpXyK</a>.",
|
||||
"Fulfill the below <a href='https://en.wikipedia.org/wiki/User_story' target='_blank'>user stories</a> and get all of the tests to pass. Give it your own personal style.",
|
||||
"You can use HTML, JavaScript, CSS, and the D3 svg-based visualization library. The tests require axes to be generated using the D3 axis property, which automatically generates ticks along the axis. These ticks are required for passing the D3 tests because their positions are used to determine alignment of graphed elements. You will find information about generating axes at <a href='https://github.com/d3/d3/blob/master/API.md#axes-d3-axis' target='_blank'>https://github.com/d3/d3/blob/master/API.md#axes-d3-axis</a>. Required (non-virtual) DOM elements are queried on the moment of each test. If you use a frontend framework (like Vue for example), the test results may be inaccurate for dynamic content. We hope to accommodate them eventually, but these frameworks are not currently supported for D3 projects.",
|
||||
"<strong>User Story #1:</strong> I can see a title element that has a corresponding <code>id=\"title\"</code>.",
|
||||
"<strong>User Story #2:</strong> I can see an x-axis that has a corresponding <code>id=\"x-axis\"</code>.",
|
||||
"<strong>User Story #3:</strong> I can see a y-axis that has a corresponding <code>id=\"y-axis\"</code>.",
|
||||
"<strong>User Story #4:</strong> I can see dots, that each have a class of <code>dot</code>, which represent the data being plotted.",
|
||||
"<strong>User Story #5:</strong> Each dot should have the properties <code>data-xvalue</code> and <code>data-yvalue</code> containing their corresponding x and y values.",
|
||||
"<strong>User Story #6:</strong> The <code>data-xvalue</code> and <code>data-yvalue</code> of each dot should be within the range of the actual data and in the correct data format. For <code>data-xvalue</code>, integers (full years) or Date objects are acceptable for test evaluation. For <code>data-yvalue</code> (minutes), use Date objects.",
|
||||
"<strong>User Story #7:</strong> The <code>data-xvalue</code> and its corresponding dot should align with the corresponding point/value on the x-axis.",
|
||||
"<strong>User Story #8:</strong> The <code>data-yvalue</code> and its corresponding dot should align with the corresponding point/value on the y-axis.",
|
||||
"<strong>User Story #9:</strong> I can see multiple tick labels on the y-axis with <code>%M:%S</code> time format.",
|
||||
"<strong>User Story #10:</strong> I can see multiple tick labels on the x-axis that show the year.",
|
||||
"<strong>User Story #11:</strong> I can see that the range of the x-axis labels are within the range of the actual x-axis data.",
|
||||
"<strong>User Story #12:</strong> I can see that the range of the y-axis labels are within the range of the actual y-axis data.",
|
||||
"<strong>User Story #13:</strong> I can see a legend containing descriptive text that has <code>id=\"legend\"</code>.",
|
||||
"<strong>User Story #14:</strong> I can mouse over an area and see a tooltip with a corresponding <code>id=\"tooltip\"</code> which displays more information about the area.",
|
||||
"<strong>User Story #15:</strong> My tooltip should have a <code>data-year</code> property that corresponds to the <code>data-xvalue</code> of the active area.",
|
||||
"Here is the dataset you will need to complete this project: <code>https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/cyclist-data.json</code>",
|
||||
"You can build your project by forking <a href='https://codepen.io/freeCodeCamp/pen/MJjpwO' target='_blank'>this CodePen pen</a>. Or you can use this CDN link to run the tests in any environment you like: <code>https://gitcdn.link/repo/freeCodeCamp/testable-projects-fcc/master/build/bundle.js</code>",
|
||||
"Once you're done, submit the URL to your working project with all its tests passing.",
|
||||
"Remember to use the <a href='https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514' target='_blank'>Read-Search-Ask</a> method if you get stuck."
|
||||
],
|
||||
"challengeSeed": [],
|
||||
"tests": [],
|
||||
"isRequired": true,
|
||||
"releasedOn": "January 1, 2016",
|
||||
"type": "zipline",
|
||||
"challengeType": 3,
|
||||
"translations": {
|
||||
"es": {
|
||||
"title": "Visualiza datos utilizando un gráfico de dispersión"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "bd7188d8c242eddfaeb5bd13",
|
||||
"title": "Visualize Data with a Heat Map",
|
||||
"description": [
|
||||
"<strong>Objective:</strong> Build a <a href='https://codepen.io' target='_blank'>CodePen.io</a> app that is functionally similar to this: <a href='https://codepen.io/freeCodeCamp/full/JEXgeY' target='_blank'>https://codepen.io/freeCodeCamp/full/JEXgeY</a>.",
|
||||
"Fulfill the below <a href='https://en.wikipedia.org/wiki/User_story' target='_blank'>user stories</a> and get all of the tests to pass. Give it your own personal style.",
|
||||
"You can use HTML, JavaScript, CSS, and the D3 svg-based visualization library. Required (non-virtual) DOM elements are queried on the moment of each test. If you use a frontend framework (like Vue for example), the test results may be inaccurate for dynamic content. We hope to accommodate them eventually, but these frameworks are not currently supported for D3 projects.",
|
||||
"<strong>User Story #1:</strong> My heat map should have a title with a corresponding <code>id=\"title\"</code>.",
|
||||
"<strong>User Story #2:</strong> My heat map should have a description with a corresponding <code>id=\"description\"</code>.",
|
||||
"<strong>User Story #3:</strong> My heat map should have an x-axis with a corresponding <code>id=\"x-axis\"</code>.",
|
||||
"<strong>User Story #4:</strong> My heat map should have a y-axis with a corresponding <code>id=\"y-axis\"</code>.",
|
||||
"<strong>User Story #5:</strong> My heat map should have <code>rect</code> elements with a <code>class=\"cell\"</code> that represent the data.",
|
||||
"<strong>User Story #6:</strong> There should be at least 4 different fill colors used for the cells.",
|
||||
"<strong>User Story #7:</strong> Each cell will have the properties <code>data-month</code>, <code>data-year</code>, <code>data-temp</code> containing their corresponding month, year, and temperature values.",
|
||||
"<strong>User Story #8:</strong> The <code>data-month</code>, <code>data-year</code> of each cell should be within the range of the data.",
|
||||
"<strong>User Story #9:</strong> My heat map should have cells that align with the corresponding month on the y-axis.",
|
||||
"<strong>User Story #10:</strong> My heat map should have cells that align with the corresponding year on the x-axis.",
|
||||
"<strong>User Story #11:</strong> My heat map should have multiple tick labels on the y-axis with the full month name.",
|
||||
"<strong>User Story #12:</strong> My heat map should have multiple tick labels on the x-axis with the years between 1754 and 2015.",
|
||||
"<strong>User Story #13:</strong> My heat map should have a legend with a corresponding <code>id=\"legend\"</code>.",
|
||||
"<strong>User Story #14:</strong> My legend should contain <code>rect</code> elements.",
|
||||
"<strong>User Story #15:</strong> The <code>rect</code> elements in the legend should use at least 4 different fill colors.",
|
||||
"<strong>User Story #16:</strong> I can mouse over an area and see a tooltip with a corresponding <code>id=\"tooltip\"</code> which displays more information about the area.",
|
||||
"<strong>User Story #16:</strong> My tooltip should have a <code>data-year</code> property that corresponds to the <code>data-year</code> of the active area.",
|
||||
"Here is the dataset you will need to complete this project: <code>https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/global-temperature.json</code>",
|
||||
"You can build your project by forking <a href='https://codepen.io/freeCodeCamp/pen/MJjpwO' target='_blank'>this CodePen pen</a>. Or you can use this CDN link to run the tests in any environment you like: <code>https://gitcdn.link/repo/freeCodeCamp/testable-projects-fcc/master/build/bundle.js</code>",
|
||||
"Once you're done, submit the URL to your working project with all its tests passing.",
|
||||
"Remember to use the <a href='https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514' target='_blank'>Read-Search-Ask</a> method if you get stuck."
|
||||
],
|
||||
"challengeSeed": [],
|
||||
"tests": [],
|
||||
"isRequired": true,
|
||||
"releasedOn": "January 1, 2016",
|
||||
"type": "zipline",
|
||||
"challengeType": 3,
|
||||
"translations": {
|
||||
"es": {
|
||||
"title": "Visualiza datos utilizando un mapa de calor"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "587d7fa6367417b2b2512bbf",
|
||||
"title": "Visualize Data with a Choropleth Map",
|
||||
"description": [
|
||||
"<strong>Objective:</strong> Build a <a href='https://codepen.io' target='_blank'>CodePen.io</a> app that is functionally similar to this: <a href='https://codepen.io/freeCodeCamp/full/EZKqza' target='_blank'>https://codepen.io/freeCodeCamp/full/EZKqza</a>.",
|
||||
"Fulfill the below <a href='https://en.wikipedia.org/wiki/User_story' target='_blank'>user stories</a> and get all of the tests to pass. Give it your own personal style.",
|
||||
"You can use HTML, JavaScript, CSS, and the D3 svg-based visualization library. Required (non-virtual) DOM elements are queried on the moment of each test. If you use a frontend framework (like Vue for example), the test results may be inaccurate for dynamic content. We hope to accommodate them eventually, but these frameworks are not currently supported for D3 projects.",
|
||||
"<strong>User Story #1:</strong> My choropleth should have a title with a corresponding <code>id=\"title\"</code>.",
|
||||
"<strong>User Story #2:</strong> My choropleth should have a description element with a corresponding <code>id=\"description\"</code>.",
|
||||
"<strong>User Story #3:</strong> My choropleth should have counties with a corresponding <code>class=\"county\"</code> that represent the data.",
|
||||
"<strong>User Story #4:</strong> There should be at least 4 different fill colors used for the counties.",
|
||||
"<strong>User Story #5:</strong> My counties should each have <code>data-fips</code> and <code>data-education</code> properties containing their corresponding fips and education values.",
|
||||
"<strong>User Story #6:</strong> My choropleth should have a county for each provided data point.",
|
||||
"<strong>User Story #7:</strong> The counties should have data-fips and data-education values that match the sample data.",
|
||||
"<strong>User Story #8:</strong> My choropleth should have a legend with a corresponding <code>id=\"legend\"</code>.",
|
||||
"<strong>User Story #9:</strong> There should be at least 4 different fill colors used for the legend.",
|
||||
"<strong>User Story #10:</strong> I can mouse over an area and see a tooltip with a corresponding <code>id=\"tooltip\"</code> which displays more information about the area.",
|
||||
"<strong>User Story #11:</strong> My tooltip should have a <code>data-education</code> property that corresponds to the <code>data-education</code> of the active area.",
|
||||
"Here are the datasets you will need to complete this project:<br><ul><li><strong>US Education Data: </strong><code>https://raw.githubusercontent.com/no-stack-dub-sack/testable-projects-fcc/master/src/data/choropleth_map/for_user_education.json</code></li><li><strong>US County Data: </strong><code>https://raw.githubusercontent.com/no-stack-dub-sack/testable-projects-fcc/master/src/data/choropleth_map/counties.json</code></li></ul>",
|
||||
"You can build your project by forking <a href='https://codepen.io/freeCodeCamp/pen/MJjpwO' target='_blank'>this CodePen pen</a>. Or you can use this CDN link to run the tests in any environment you like: <code>https://gitcdn.link/repo/freeCodeCamp/testable-projects-fcc/master/build/bundle.js</code>",
|
||||
"Once you're done, submit the URL to your working project with all its tests passing.",
|
||||
"Remember to use the <a href='https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514' target='_blank'>Read-Search-Ask</a> method if you get stuck."
|
||||
],
|
||||
"challengeSeed": [],
|
||||
"tests": [],
|
||||
"isRequired": true,
|
||||
"releasedOn": "February 17, 2017",
|
||||
"type": "zipline",
|
||||
"challengeType": 3,
|
||||
"translations": {}
|
||||
},
|
||||
{
|
||||
"id": "587d7fa6367417b2b2512bc0",
|
||||
"title": "Visualize Data with a Treemap Diagram",
|
||||
"description": [
|
||||
"<strong>Objective:</strong> Build a <a href='https://codepen.io' target='_blank'>CodePen.io</a> app that is functionally similar to this: <a href='https://codepen.io/freeCodeCamp/full/KaNGNR' target='_blank'>https://codepen.io/freeCodeCamp/full/KaNGNR</a>.",
|
||||
"Fulfill the below <a href='https://en.wikipedia.org/wiki/User_story' target='_blank'>user stories</a> and get all of the tests to pass. Give it your own personal style.",
|
||||
"You can use HTML, JavaScript, CSS, and the D3 svg-based visualization library. The tests require axes to be generated using the D3 axis property, which automatically generates ticks along the axis. These ticks are required for passing the D3 tests because their positions are used to determine alignment of graphed elements. You will find information about generating axes at <a href='https://github.com/d3/d3/blob/master/API.md#axes-d3-axis' target='_blank'>https://github.com/d3/d3/blob/master/API.md#axes-d3-axis</a>. Required (non-virtual) DOM elements are queried on the moment of each test. If you use a frontend framework (like Vue for example), the test results may be inaccurate for dynamic content. We hope to accommodate them eventually, but these frameworks are not currently supported for D3 projects.",
|
||||
"<strong>User Story #1:</strong> My tree map should have a title with a corresponding <code>id=\"title\"</code>.",
|
||||
"<strong>User Story #2:</strong> My tree map should have a description with a corresponding <code>id=\"description\"</code>.",
|
||||
"<strong>User Story #3:</strong> My tree map should have <code>rect</code> elements with a corresponding <code>class=\"tile\"</code> that represent the data.",
|
||||
"<strong>User Story #4:</strong> There should be at least 2 different fill colors used for the tiles.",
|
||||
"<strong>User Story #5:</strong> Each tile should have the properties <code>data-name</code>, <code>data-category</code>, and <code>data-value</code> containing their corresponding name, category, and value.",
|
||||
"<strong>User Story #6:</strong> The area of each tile should correspond to the data-value amount: tiles with a larger data-value should have a bigger area.",
|
||||
"<strong>User Story #7:</strong> My tree map should have a legend with corresponding <code>id=\"legend\"</code>.",
|
||||
"<strong>User Story #8:</strong> My legend should have <code>rect</code> elements with a corresponding <code>class=\"legend-item\"</code>.",
|
||||
"<strong>User Story #9:</strong> The <code>rect</code> elements in the legend should use at least 2 different fill colors.",
|
||||
"<strong>User Story #10:</strong> I can mouse over an area and see a tooltip with a corresponding <code>id=\"tooltip\"</code> which displays more information about the area.",
|
||||
"<strong>User Story #11:</strong> My tooltip should have a <code>data-value</code> property that corresponds to the <code>data-value</code> of the active area.",
|
||||
"For this project you can use any of the following datasets:<br><ul><li><strong>Kickstarter Pledges:</strong> <code>https://cdn.rawgit.com/freeCodeCamp/testable-projects-fcc/a80ce8f9/src/data/tree_map/kickstarter-funding-data.json</code></li><li><strong>Movie Sales:</strong> <code>https://cdn.rawgit.com/freeCodeCamp/testable-projects-fcc/a80ce8f9/src/data/tree_map/movie-data.json</code></li><li><strong>Video Game Sales:</strong> <code>https://cdn.rawgit.com/freeCodeCamp/testable-projects-fcc/a80ce8f9/src/data/tree_map/video-game-sales-data.json</code></li></ul>",
|
||||
"You can build your project by forking <a href='https://codepen.io/freeCodeCamp/pen/MJjpwO' target='_blank'>this CodePen pen</a>. Or you can use this CDN link to run the tests in any environment you like: <code>https://gitcdn.link/repo/freeCodeCamp/testable-projects-fcc/master/build/bundle.js</code>",
|
||||
"Once you're done, submit the URL to your working project with all its tests passing.",
|
||||
"Remember to use the <a href='https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck/19514' target='_blank'>Read-Search-Ask</a> method if you get stuck."
|
||||
],
|
||||
"challengeSeed": [],
|
||||
"tests": [],
|
||||
"isRequired": true,
|
||||
"releasedOn": "February 17, 2017",
|
||||
"type": "zipline",
|
||||
"challengeType": 3,
|
||||
"translations": {}
|
||||
}
|
||||
],
|
||||
"fileName": "04-data-visualization/data-visualization-projects.json",
|
||||
"superBlock": "data-visualization",
|
||||
"superOrder": 4
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,834 @@
|
||||
{
|
||||
"name": "JSON APIs and AJAX",
|
||||
"order": 2,
|
||||
"time": "2 hours",
|
||||
"helpRoom": "Help",
|
||||
"required": [
|
||||
{
|
||||
"link": "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css"
|
||||
}
|
||||
],
|
||||
"challenges": [
|
||||
{
|
||||
"id": "587d7fad367417b2b2512be1",
|
||||
"title": "Handle Click Events with JavaScript using the onclick property",
|
||||
"description": [
|
||||
"You want your code to execute only once your page has finished loading. For that purpose, you can attach a JavaScript event to the document called <code>DOMContentLoaded</code>. Here's the code that does this:",
|
||||
"<blockquote>document.addEventListener('DOMContentLoaded',function() {<br><br>});</blockquote>",
|
||||
"You can implement event handlers that go inside of the <code>DOMContentLoaded</code> function. You can implement an <code>onclick</code> event handler which triggers when the user clicks on the element with id <code>getMessage</code>, by adding the following code:",
|
||||
"<blockquote>document.getElementById('getMessage').onclick=function(){};</blockquote>",
|
||||
"<hr>",
|
||||
"Add a click event handler inside of the <code>DOMContentLoaded</code> function for the element with id of <code>getMessage</code>."
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"text": "Your code should use the <code>document.getElementById</code> method to select the <code>getMessage</code> element.",
|
||||
"testString": "assert(code.match(/document\\.getElementById\\(\\s*?('|\")getMessage\\1\\s*?\\)/g), 'Your code should use the <code>document.getElementById</code> method to select the <code>getMessage</code> element.');"
|
||||
},
|
||||
{
|
||||
"text": "Your code should add an <code>onclick</code> event handler.",
|
||||
"testString": "assert(typeof document.getElementById('getMessage').onclick === 'function', 'Your code should add an <code>onclick</code> event handler.');"
|
||||
}
|
||||
],
|
||||
"solutions": [],
|
||||
"hints": [],
|
||||
"type": "waypoint",
|
||||
"releasedOn": "Feb 17, 2017",
|
||||
"challengeType": 0,
|
||||
"translations": {},
|
||||
"files": {
|
||||
"indexjs": {
|
||||
"key": "indexhtml",
|
||||
"ext": "html",
|
||||
"name": "index",
|
||||
"contents": [
|
||||
"<script>",
|
||||
" document.addEventListener('DOMContentLoaded',function(){",
|
||||
" // Add your code below this line",
|
||||
" ",
|
||||
" ",
|
||||
" // 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>"
|
||||
],
|
||||
"head": "",
|
||||
"tail": ""
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "587d7fad367417b2b2512be2",
|
||||
"title": "Change Text with click Events",
|
||||
"description": [
|
||||
"When the click event happens, you can use JavaScript to update an HTML element.",
|
||||
"For example, when a user clicks the \"Get Message\" button, it changes the text of the element with the class <code>message</code> to say \"Here is the message\".",
|
||||
"This works by adding the following code within the click event:",
|
||||
"<code>document.getElementsByClassName('message')[0].innerHTML=\"Here is the message\";</code>",
|
||||
"<hr>",
|
||||
"Add code inside the <code>onclick</code> event handler to change the text inside the <code>message</code> element to say \"Here is the message\"."
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"text": "Your code should use the <code>document.getElementsByClassName</code> method to select the element with class <code>message</code> and set its <code>innerHTML</code> to the given string.",
|
||||
"testString": "assert(code.match(/document\\.getElementsByClassName\\(\\s*?('|\")message\\1\\s*?\\)\\[0\\]\\.innerHTML\\s*?=\\s*?('|\")Here is the message\\2/g), 'Your code should use the <code>document.getElementsByClassName</code> method to select the element with class <code>message</code> and set its <code>innerHTML</code> to the given string.');"
|
||||
}
|
||||
],
|
||||
"solutions": [],
|
||||
"hints": [],
|
||||
"type": "waypoint",
|
||||
"releasedOn": "Feb 17, 2017",
|
||||
"challengeType": 0,
|
||||
"translations": {},
|
||||
"files": {
|
||||
"indexjs": {
|
||||
"key": "indexhtml",
|
||||
"ext": "html",
|
||||
"name": "index",
|
||||
"contents": [
|
||||
"<script>",
|
||||
" document.addEventListener('DOMContentLoaded',function(){",
|
||||
" document.getElementById('getMessage').onclick=function(){",
|
||||
" // Add your code below this line",
|
||||
" ",
|
||||
" ",
|
||||
" // 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>"
|
||||
],
|
||||
"head": "",
|
||||
"tail": ""
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "587d7fae367417b2b2512be3",
|
||||
"title": "Get JSON with the JavaScript XMLHttpRequest Method",
|
||||
"description": [
|
||||
"You can also request data from an external source. This is where APIs come into play.",
|
||||
"Remember that APIs - or Application Programming Interfaces - are tools that computers use to communicate with one another. You'll learn how to update HTML with the data we get from APIs using a technology called AJAX.",
|
||||
"Most web APIs transfer data in a format called JSON. JSON stands for JavaScript Object Notation.",
|
||||
"JSON syntax looks very similar to JavaScript object literal notation. JSON has object properties and their current values, sandwiched between a <code>{</code> and a <code>}</code>.",
|
||||
"These properties and their values are often referred to as \"key-value pairs\".",
|
||||
"However, JSON transmitted by APIs are sent as <code>bytes</code>, and your application receives it as a <code>string</code>. These can be converted into JavaScript objects, but they are not JavaScript objects by default. The <code>JSON.parse</code> method parses the string and constructs the JavaScript object described by it.",
|
||||
"You can request the JSON from freeCodeCamp's Cat Photo API. Here's the code you can put in your click event to do this:",
|
||||
"<blockquote>req=new XMLHttpRequest();<br>req.open(\"GET\",'/json/cats.json',true);<br>req.send();<br>req.onload=function(){<br> json=JSON.parse(req.responseText);<br> document.getElementsByClassName('message')[0].innerHTML=JSON.stringify(json);<br>};</blockquote>",
|
||||
"Here's a review of what each piece is doing. The JavaScript <code>XMLHttpRequest</code> object has a number of properties and methods that are used to transfer data. First, an instance of the <code>XMLHttpRequest</code> object is created and saved in the <code>req</code> variable.",
|
||||
"Next, the <code>open</code> method initializes a request - this example is requesting data from an API, therefore is a \"GET\" request. The second argument for <code>open</code> is the URL of the API you are requesting data from. The third argument is a Boolean value where <code>true</code> makes it an asynchronous request.",
|
||||
"The <code>send</code> method sends the request. Finally, the <code>onload</code> event handler parses the returned data and applies the <code>JSON.stringify</code> method to convert the JavaScript object into a string. This string is then inserted as the message text.",
|
||||
"<hr>",
|
||||
"Update the code to create and send a \"GET\" request to the freeCodeCamp Cat Photo API. Then click the \"Get Message\" button. Your AJAX function will replace the \"The message will go here\" text with the raw JSON output from the API."
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"text": "Your code should create a new <code>XMLHttpRequest</code>.",
|
||||
"testString": "assert(code.match(/new\\s+?XMLHttpRequest\\(\\s*?\\)/g), 'Your code should create a new <code>XMLHttpRequest</code>.');"
|
||||
},
|
||||
{
|
||||
"text": "Your code should use the <code>open</code> method to initialize a \"GET\" request to the freeCodeCamp Cat Photo API.",
|
||||
"testString": "assert(code.match(/\\.open\\(\\s*?('|\")GET\\1\\s*?,\\s*?('|\")\\/json\\/cats\\.json\\2\\s*?,\\s*?true\\s*?\\)/g), 'Your code should use the <code>open</code> method to initialize a \"GET\" request to the freeCodeCamp Cat Photo API.');"
|
||||
},
|
||||
{
|
||||
"text": "Your code should use the <code>send</code> method to send the request.",
|
||||
"testString": "assert(code.match(/\\.send\\(\\s*\\)/g), 'Your code should use the <code>send</code> method to send the request.');"
|
||||
},
|
||||
{
|
||||
"text": "Your code should have an <code>onload</code> event handler set to a function.",
|
||||
"testString": "assert(code.match(/\\.onload\\s*=\\s*function\\(\\s*?\\)\\s*?{/g), 'Your code should have an <code>onload</code> event handler set to a function.');"
|
||||
},
|
||||
{
|
||||
"text": "Your code should use the <code>JSON.parse</code> method to parse the <code>responseText</code>.",
|
||||
"testString": "assert(code.match(/JSON\\.parse\\(.*\\.responseText\\)/g), 'Your code should use the <code>JSON.parse</code> method to parse the <code>responseText</code>.');"
|
||||
},
|
||||
{
|
||||
"text": "Your code should get the element with class <code>message</code> and change its inner HTML to the string of JSON data.",
|
||||
"testString": "assert(code.match(/document\\.getElementsByClassName\\(\\s*?('|\")message\\1\\s*?\\)\\[0\\]\\.innerHTML\\s*?=\\s*?JSON\\.stringify\\(.+?\\)/g), 'Your code should get the element with class <code>message</code> and change its inner HTML to the string of JSON data.');"
|
||||
}
|
||||
],
|
||||
"solutions": [],
|
||||
"hints": [],
|
||||
"type": "waypoint",
|
||||
"releasedOn": "Feb 17, 2017",
|
||||
"challengeType": 0,
|
||||
"translations": {},
|
||||
"files": {
|
||||
"indexjs": {
|
||||
"key": "indexhtml",
|
||||
"ext": "html",
|
||||
"name": "index",
|
||||
"contents": [
|
||||
"<script>",
|
||||
" document.addEventListener('DOMContentLoaded',function(){",
|
||||
" document.getElementById('getMessage').onclick=function(){",
|
||||
" // Add your code below this line",
|
||||
" ",
|
||||
" ",
|
||||
" // 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>"
|
||||
],
|
||||
"head": "",
|
||||
"tail": ""
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "587d7fae367417b2b2512be4",
|
||||
"title": "Access the JSON Data from an API",
|
||||
"description": [
|
||||
"In the previous challenge, you saw how to get JSON data from the freeCodeCamp Cat Photo API.",
|
||||
"Now you'll take a closer look at the returned data to better understand the JSON format. Recall some notation in JavaScript:",
|
||||
"<blockquote>[ ] -> Square brackets represent an array<br>{ } -> Curly brackets represent an object<br>\" \" -> Double quotes represent a string. They are also used for key names in JSON</blockquote>",
|
||||
"Understanding the structure of the data that an API returns is important because it influences how you retrieve the values you need.",
|
||||
"On the right, click the \"Get Message\" button to load the freeCodeCamp Cat Photo API JSON into the HTML.",
|
||||
"The first and last character you see in the JSON data are square brackets <code>[ ]</code>. This means that the returned data is an array. The second character in the JSON data is a curly <code>{</code> bracket, which starts an object. Looking closely, you can see that there are three separate objects. The JSON data is an array of three objects, where each object contains information about a cat photo.",
|
||||
"You learned earlier that objects contain \"key-value pairs\" that are separated by commas. In the Cat Photo example, the first object has <code>\"id\":0</code> where \"id\" is a key and 0 is its corresponding value. Similarly, there are keys for \"imageLink\", \"altText\", and \"codeNames\". Each cat photo object has these same keys, but with different values.",
|
||||
"Another interesting \"key-value pair\" in the first object is <code>\"codeNames\":[\"Juggernaut\",\"Mrs. Wallace\",\"ButterCup\"]</code>. Here \"codeNames\" is the key and its value is an array of three strings. It's possible to have arrays of objects as well as a key with an array as a value.",
|
||||
"Remember how to access data in arrays and objects. Arrays use bracket notation to access a specific index of an item. Objects use either bracket or dot notation to access the value of a given property. Here's an example that prints the \"altText\" of the first cat photo - note that the parsed JSON data in the editor is saved in a variable called <code>json</code>:",
|
||||
"<blockquote>console.log(json[0].altText);<br>// Prints \"A white cat wearing a green helmet shaped melon on its head.\"</blockquote>",
|
||||
"<hr>",
|
||||
"For the cat with the \"id\" of 2, print to the console the second value in the <code>codeNames</code> array. You should use bracket and dot notation on the object (which is saved in the variable <code>json</code>) to access the value."
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"text": "Your code should use bracket and dot notation to access the proper code name, and print \"Loki\" to the console.",
|
||||
"testString": "assert(code.match(/(?:json\\[2\\]\\.codeNames\\[1\\]|json\\[2\\]\\[('|\")codeNames\\1\\]\\[1\\])/g), 'Your code should use bracket and dot notation to access the proper code name, and print \"Loki\" to the console.');"
|
||||
}
|
||||
],
|
||||
"solutions": [],
|
||||
"hints": [],
|
||||
"type": "waypoint",
|
||||
"releasedOn": "Feb 17, 2017",
|
||||
"challengeType": 0,
|
||||
"translations": {},
|
||||
"files": {
|
||||
"indexjs": {
|
||||
"key": "indexhtml",
|
||||
"ext": "html",
|
||||
"name": "index",
|
||||
"contents": [
|
||||
"<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",
|
||||
" ",
|
||||
" ",
|
||||
" // 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>"
|
||||
],
|
||||
"head": "",
|
||||
"tail": ""
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "587d7fae367417b2b2512be5",
|
||||
"title": "Convert JSON Data to HTML",
|
||||
"description": [
|
||||
"Now that you're getting data from a JSON API, you can display it in the HTML.",
|
||||
"You can use a <code>forEach</code> method to loop through the data since the cat photo objects are held in an array. As you get to each item, you can modify the HTML elements.",
|
||||
"First, declare an html variable with <code>var html = \"\";</code>.",
|
||||
"Then, loop through the JSON, adding HTML to the variable that wraps the key names in <code>strong</code> tags, followed by the value. When the loop is finished, you render it.",
|
||||
"Here's the code that does this:",
|
||||
"<blockquote>json.forEach(function(val) {</br> var keys = Object.keys(val);</br> html += \"<div class = 'cat'>\";</br> keys.forEach(function(key) {</br> html += \"<strong>\" + key + \"</strong>: \" + val[key] + \"<br>\";</br> });</br> html += \"</div><br>\";</br>});</blockquote>",
|
||||
"<hr>",
|
||||
"Add a <code>forEach</code> method to loop over the JSON data and create the HTML elements to display it.",
|
||||
"Here is some example JSON",
|
||||
"<blockquote>[</br> {</br> \"id\":0,</br> \"imageLink\":\"https://s3.amazonaws.com/freecodecamp/funny-cat.jpg\",</br> \"altText\":\"A white cat wearing a green helmet shaped melon on its head. \",</br> \"codeNames\":[</br> \"Juggernaut\",</br> \"Mrs. Wallace\",</br> \"Buttercup\"</br> ]</br> }</br>]</blockquote>"
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"text": "Your code should store the data in the <code>html</code> variable",
|
||||
"testString": "assert(code.match(/html\\s+?(\\+=|=\\shtml\\s\\+)/g), 'Your code should store the data in the <code>html</code> variable');"
|
||||
},
|
||||
{
|
||||
"text": "Your code should use a <code>forEach</code> method to loop over the JSON data from the API.",
|
||||
"testString": "assert(code.match(/json\\.forEach/g), 'Your code should use a <code>forEach</code> method to loop over the JSON data from the API.');"
|
||||
},
|
||||
{
|
||||
"text": "Your code should wrap the key names in <code>strong</code> tags.",
|
||||
"testString": "assert(code.match(/<strong>.+<\\/strong>/g), 'Your code should wrap the key names in <code>strong</code> tags.');"
|
||||
}
|
||||
],
|
||||
"solutions": [],
|
||||
"hints": [],
|
||||
"type": "waypoint",
|
||||
"releasedOn": "Feb 17, 2017",
|
||||
"challengeType": 0,
|
||||
"translations": {},
|
||||
"files": {
|
||||
"indexjs": {
|
||||
"key": "indexhtml",
|
||||
"ext": "html",
|
||||
"name": "index",
|
||||
"contents": [
|
||||
"<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);",
|
||||
" var html = \"\";",
|
||||
" // Add your code below this line",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" // Add your code above this line",
|
||||
" document.getElementsByClassName('message')[0].innerHTML=html;",
|
||||
" };",
|
||||
" };",
|
||||
" });",
|
||||
"</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>"
|
||||
],
|
||||
"head": "",
|
||||
"tail": ""
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "587d7fae367417b2b2512be6",
|
||||
"title": "Render Images from Data Sources",
|
||||
"description": [
|
||||
"The last few challenges showed that each object in the JSON array contains an <code>imageLink</code> key with a value that is the URL of a cat's image.",
|
||||
"When you're looping through these objects, you can use this <code>imageLink</code> property to display this image in an <code>img</code> element.",
|
||||
"Here's the code that does this:",
|
||||
"<code>html += \"<img src = '\" + val.imageLink + \"' \" + \"alt='\" + val.altText + \"'>\";</code>",
|
||||
"<hr>",
|
||||
"Add code to use the <code>imageLink</code> and <code>altText</code> properties in an <code>img</code> tag."
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"text": "You should use the <code>imageLink</code> property to display the images.",
|
||||
"testString": "assert(code.match(/val\\.imageLink/g), 'You should use the <code>imageLink</code> property to display the images.');"
|
||||
}
|
||||
],
|
||||
"solutions": [],
|
||||
"hints": [],
|
||||
"type": "waypoint",
|
||||
"releasedOn": "Feb 17, 2017",
|
||||
"challengeType": 0,
|
||||
"translations": {},
|
||||
"files": {
|
||||
"indexjs": {
|
||||
"key": "indexhtml",
|
||||
"ext": "html",
|
||||
"name": "index",
|
||||
"contents": [
|
||||
"<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);",
|
||||
" var html = \"\";",
|
||||
" json.forEach(function(val) {",
|
||||
" html += \"<div class = 'cat'>\";",
|
||||
" // Add your code below this line",
|
||||
" ",
|
||||
" ",
|
||||
" // Add your code above this line",
|
||||
" html += \"</div><br>\";",
|
||||
" });",
|
||||
" document.getElementsByClassName('message')[0].innerHTML=html;",
|
||||
" };",
|
||||
" };",
|
||||
" });",
|
||||
"</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>"
|
||||
],
|
||||
"head": "",
|
||||
"tail": ""
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "587d7fae367417b2b2512be7",
|
||||
"title": "Pre-filter JSON to Get the Data You Need",
|
||||
"description": [
|
||||
"If you don't want to render every cat photo you get from the freeCodeCamp Cat Photo API, you can pre-filter the JSON before looping through it.",
|
||||
"Given that the JSON data is stored in an array, you can use the <code>filter</code> method to filter out the cat whose \"id\" key has a value of 1.",
|
||||
"Here's the code to do this:",
|
||||
"<blockquote>json = json.filter(function(val) {<br> return (val.id !== 1);<br>});</blockquote>",
|
||||
"<hr>",
|
||||
"Add code to <code>filter</code> the json data to remove the cat with the \"id\" value of 1."
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"text": "Your code should use the <code>filter</code> method.",
|
||||
"testString": "assert(code.match(/json\\.filter/g), 'Your code should use the <code>filter</code> method.');"
|
||||
}
|
||||
],
|
||||
"solutions": [],
|
||||
"hints": [],
|
||||
"type": "waypoint",
|
||||
"releasedOn": "Feb 17, 2017",
|
||||
"challengeType": 0,
|
||||
"translations": {},
|
||||
"files": {
|
||||
"indexjs": {
|
||||
"key": "indexhtml",
|
||||
"ext": "html",
|
||||
"name": "index",
|
||||
"contents": [
|
||||
"<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);",
|
||||
" var html = \"\";",
|
||||
" // Add your code below this line",
|
||||
" ",
|
||||
" ",
|
||||
" // Add your code above this line",
|
||||
" json.forEach(function(val) {",
|
||||
" html += \"<div class = 'cat'>\"",
|
||||
" ",
|
||||
" html += \"<img src = '\" + val.imageLink + \"' \" + \"alt='\" + val.altText + \"'>\"",
|
||||
" ",
|
||||
" html += \"</div>\"",
|
||||
" });",
|
||||
" document.getElementsByClassName('message')[0].innerHTML=html;",
|
||||
" };",
|
||||
" }; ",
|
||||
" });",
|
||||
"</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>"
|
||||
],
|
||||
"head": "",
|
||||
"tail": ""
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "587d7faf367417b2b2512be8",
|
||||
"title": "Get Geolocation Data to Find A User's GPS Coordinates",
|
||||
"description": [
|
||||
"Another cool thing you can do is access your user's current location. Every browser has a built in navigator that can give you this information.",
|
||||
"The navigator will get the user's current longitude and latitude.",
|
||||
"You will see a prompt to allow or block this site from knowing your current location. The challenge can be completed either way, as long as the code is correct.",
|
||||
"By selecting allow, you will see the text on the output phone change to your latitude and longitude.",
|
||||
"Here's code that does this:",
|
||||
"<blockquote>if (navigator.geolocation){<br> navigator.geolocation.getCurrentPosition(function(position) {<br> document.getElementById('data').innerHTML=\"latitude: \"+ position.coords.latitude + \"<br>longitude: \" + position.coords.longitude;<br> });<br>}</blockquote>",
|
||||
"First, it checks if the <code>navigator.geolocation</code> object exists. If it does, the <code>getCurrentPosition</code> method on that object is called, which initiates an asynchronous request for the user's position. If the request is successful, the callback function in the method runs. This function accesses the <code>position</code> object's values for latitude and longitude using dot notation and updates the HTML.",
|
||||
"<hr>",
|
||||
"Add the example code inside the <code>script</code> tags to check a user's current location and insert it into the HTML."
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"text": "Your code should use <code>navigator.geolocation</code> to access the user's current location.",
|
||||
"testString": "assert(code.match(/navigator\\.geolocation\\.getCurrentPosition/g), 'Your code should use <code>navigator.geolocation</code> to access the user's current location.');"
|
||||
},
|
||||
{
|
||||
"text": "Your code should use <code>position.coords.latitude</code> to display the user's latitudinal location.",
|
||||
"testString": "assert(code.match(/position\\.coords\\.latitude/g), 'Your code should use <code>position.coords.latitude</code> to display the user's latitudinal location.');"
|
||||
},
|
||||
{
|
||||
"text": "Your code should use <code>position.coords.longitude</code> to display the user's longitudinal location.",
|
||||
"testString": "assert(code.match(/position\\.coords\\.longitude/g), 'Your code should use <code>position.coords.longitude</code> to display the user's longitudinal location.');"
|
||||
},
|
||||
{
|
||||
"text": "You should display the user's position within the <code>data</code> div element.",
|
||||
"testString": "assert(code.match(/document\\.getElementById\\(\\s*?('|\")data\\1\\s*?\\)\\.innerHTML/g), 'You should display the user's position within the <code>data</code> div element.');"
|
||||
}
|
||||
],
|
||||
"solutions": [],
|
||||
"hints": [],
|
||||
"type": "waypoint",
|
||||
"releasedOn": "Feb 17, 2017",
|
||||
"challengeType": 0,
|
||||
"translations": {},
|
||||
"files": {
|
||||
"indexjs": {
|
||||
"key": "indexhtml",
|
||||
"ext": "html",
|
||||
"name": "index",
|
||||
"contents": [
|
||||
"<script>",
|
||||
" // Add your code below this line",
|
||||
" ",
|
||||
" ",
|
||||
" // Add your code above this line",
|
||||
"</script>",
|
||||
"<h4>You are here:</h4>",
|
||||
"<div id=\"data\">",
|
||||
"",
|
||||
"</div>"
|
||||
],
|
||||
"head": "",
|
||||
"tail": ""
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "587d7faf367417b2b2512be9",
|
||||
"title": "Post Data with the JavaScript XMLHttpRequest Method",
|
||||
"description": [
|
||||
"In the previous examples, you received data from an external resource. You can also send data to an external resource, as long as that resource supports AJAX requests and you know the URL.",
|
||||
"JavaScript's <code>XMLHttpRequest</code> method is also used to post data to a server. Here's an example:",
|
||||
"<blockquote>req=new XMLHttpRequest();<br>req.open(\"POST\",url,true);<br>req.setRequestHeader('Content-Type','text/plain');<br>req.onreadystatechange=function(){<br> if(req.readyState==4 && req.status==200){<br> document.getElementsByClassName('message')[0].innerHTML=req.responseText;<br> }<br>};<br>req.send(userName);</blockquote>",
|
||||
"You've seen several of these methods before. Here the <code>open</code> method initializes the request as a \"POST\" to the given URL of the external resource, and uses the <code>true</code> Boolean to make it asynchronous.",
|
||||
"The <code>setRequestHeader</code> method sets the value of an HTTP request header, which contains information about the sender and the request. It must be called after the <code>open</code> method, but before the <code>send</code> method. The two parameters are the name of the header and the value to set as the body of that header.",
|
||||
"Next, the <code>onreadystatechange</code> event listener handles a change in the state of the request. A <code>readyState</code> of 4 means the operation is complete, and a <code>status</code> of 200 means it was a successful request. The document's HTML can be updated.",
|
||||
"Finally, the <code>send</code> method sends the request with the <code>userName</code> value, which was given by the user in the <code>input</code> field.",
|
||||
"<hr>",
|
||||
"Update the code to create and send a \"POST\" request. Then enter your name in input box and click \"Send Message\". Your AJAX function will replace \"Reply from Server will be here.\" with the reply of the server. In this case, it is your name appended with \" loves cats\"."
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"text": "Your code should create a new <code>XMLHttpRequest</code>.",
|
||||
"testString": "assert(code.match(/new\\s+?XMLHttpRequest\\(\\s*?\\)/g), 'Your code should create a new <code>XMLHttpRequest</code>.');"
|
||||
},
|
||||
{
|
||||
"text": "Your code should use the <code>open</code> method to initialize a \"POST\" request to the server.",
|
||||
"testString": "assert(code.match(/\\.open\\(\\s*?('|\")POST\\1\\s*?,\\s*?url\\s*?,\\s*?true\\s*?\\)/g), 'Your code should use the <code>open</code> method to initialize a \"POST\" request to the server.');"
|
||||
},
|
||||
{
|
||||
"text": "Your code should use the <code>setRequestHeader</code> method.",
|
||||
"testString": "assert(code.match(/\\.setRequestHeader\\(\\s*?('|\")Content-Type\\1\\s*?,\\s*?('|\")text\\/plain\\2\\s*?\\)/g), 'Your code should use the <code>setRequestHeader</code> method.');"
|
||||
},
|
||||
{
|
||||
"text": "Your code should have an <code>onreadystatechange</code> event handler set to a function.",
|
||||
"testString": "assert(code.match(/\\.onreadystatechange\\s*?=/g), 'Your code should have an <code>onreadystatechange</code> event handler set to a function.');"
|
||||
},
|
||||
{
|
||||
"text": "Your code should get the element with class <code>message</code> and change its inner HTML to the <code>responseText</code>.",
|
||||
"testString": "assert(code.match(/document\\.getElementsByClassName\\(\\s*?('|\")message\\1\\s*?\\)\\[0\\]\\.innerHTML\\s*?=\\s*?.+?\\.responseText/g), 'Your code should get the element with class <code>message</code> and change its inner HTML to the <code>responseText</code>.');"
|
||||
},
|
||||
{
|
||||
"text": "Your code should use the <code>send</code> method.",
|
||||
"testString": "assert(code.match(/\\.send\\(\\s*?userName\\s*?\\)/g), 'Your code should use the <code>send</code> method.');"
|
||||
}
|
||||
],
|
||||
"solutions": [],
|
||||
"hints": [],
|
||||
"type": "waypoint",
|
||||
"releasedOn": "Feb 17, 2017",
|
||||
"challengeType": 0,
|
||||
"translations": {},
|
||||
"files": {
|
||||
"indexjs": {
|
||||
"key": "indexhtml",
|
||||
"ext": "html",
|
||||
"name": "index",
|
||||
"contents": [
|
||||
"<script>",
|
||||
" document.addEventListener('DOMContentLoaded',function(){",
|
||||
" document.getElementById('sendMessage').onclick=function(){",
|
||||
" ",
|
||||
" var userName=document.getElementById('name').value;",
|
||||
" // Add your code below this line",
|
||||
" ",
|
||||
" ",
|
||||
" // 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 Friends</h1> ",
|
||||
"<p class=\"message box\">",
|
||||
" Reply from Server will be here",
|
||||
"</p>",
|
||||
"<p>",
|
||||
" <label for=\"name\">Your name:",
|
||||
" <input type=\"text\" id=\"name\"/>",
|
||||
" </label>",
|
||||
" <button id=\"sendMessage\">",
|
||||
" Send Message",
|
||||
" </button>",
|
||||
"</p>"
|
||||
],
|
||||
"head": "",
|
||||
"tail": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"fileName": "04-data-visualization/json-apis-and-ajax.json",
|
||||
"superBlock": "data-visualization",
|
||||
"superOrder": 4
|
||||
}
|
Reference in New Issue
Block a user