Difference between revisions of "DOM working with data"
Jump to navigation
Jump to search
(19 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
== Get the name from the number == | == Get the name from the number == | ||
*You can obtain data from the server using a fetch call. | *You can obtain data from the server using a fetch call. | ||
*In these examples you will be getting data from /worldl.json this is a list of 195 countries in json format | *In these examples you will be getting data from https://progzoo.net/worldl.json this is a list of 195 countries in json format | ||
<div class=qu> | <div class=qu> | ||
* The program snippet shows the name of country 42. | * The program snippet shows the name of country 42. | ||
Line 25: | Line 25: | ||
</div> | </div> | ||
Each country has a bunch of data associated, for example: | Each country has a bunch of data associated, for example: | ||
area: 330803 | area: 330803 | ||
Line 37: | Line 35: | ||
population: 30177000 | population: 30177000 | ||
tld: ".my" | tld: ".my" | ||
== Show the population == | |||
<div class=qu> | |||
<div class=imper>Alter your code so that the output matches the model answer.</div> | <div class=imper>Alter your code so that the output matches the model answer.</div> | ||
<pre class=usr> | <pre class=usr> | ||
Line 43: | Line 45: | ||
.then((r)=>{ | .then((r)=>{ | ||
let tgt = r[102]; | let tgt = r[102]; | ||
document.body.innerHTML = | |||
`<div>${tgt.name}</div>`; | |||
}); | |||
</pre> | |||
<pre class=ans> | |||
fetch('/worldl.json') | |||
.then((r)=>r.json()) | |||
.then((r)=>{ | |||
let tgt = r[102]; | |||
document.body.innerHTML = | |||
`<div>${tgt.name}, ${tgt.capital}</div>`; | |||
}); | |||
</pre> | |||
</div> | |||
== Include image == | |||
<div class=qu> | |||
<div class=imper>Include three div elements and an img for country 151. Use <code>style='width:100px;'</code> for the img</div> | |||
<pre class=usr> | |||
fetch('/worldl.json') | |||
.then((r)=>r.json()) | |||
.then((r)=>{ | |||
let tgt = r[151]; | |||
document.body.innerHTML = | |||
`<div>${tgt.name}, ${tgt.capital}</div>`; | |||
}); | |||
</pre> | |||
<pre class=ans> | |||
fetch('/worldl.json') | |||
.then((r)=>r.json()) | |||
.then((r)=>{ | |||
let tgt = r[151]; | |||
document.body.innerHTML = ` | document.body.innerHTML = ` | ||
<div>Name: ${tgt.name}</div> | |||
<div>Capital: ${tgt.capital}</div> | |||
<div>Population: ${tgt.population}</div> | |||
<img src='${tgt.flag}' style='width:100px'/> | |||
`; | `; | ||
}); | }); | ||
</pre> | |||
</div> | |||
== Output in a table == | |||
<div class=qu> | |||
<pre class=usr> | |||
fetch('/worldl.json') | |||
.then((r)=>r.json()) | |||
.then((r)=>{ | |||
let tgt = r[172]; | |||
document.body.innerHTML = | |||
`<div>${tgt.name}, ${tgt.capital}</div>`; | |||
}); | |||
</pre> | </pre> | ||
<pre class=ans> | <pre class=ans> | ||
Line 55: | Line 103: | ||
.then((r)=>r.json()) | .then((r)=>r.json()) | ||
.then((r)=>{ | .then((r)=>{ | ||
let tgt = r[ | let tgt = r[172]; | ||
document.body.innerHTML = ` | document.body.innerHTML = ` | ||
<table> | |||
<tr><th>Name</th><td>${tgt.name}</td</tr> | |||
<tr><th>Continent</th><td>${tgt.continent}</td</tr> | |||
<tr><th>Capital</th><td>${tgt.capital}</td</tr> | |||
<tr><th>Population</th><td>${tgt.population}</td</tr> | |||
</table> | |||
`; | `; | ||
}); | |||
</pre> | </pre> | ||
</div> | </div> | ||
== | |||
== Format numbers == | |||
<div class=qu> | <div class=qu> | ||
<div class=imper> | <div class='imper'>For country 184. Use .toLocaleString() to format numbers better. Show the GDP in billions, include $ symbol as GDP is given in US dollars</div> | ||
<pre class=usr> | <pre class=usr> | ||
fetch('/worldl.json') | fetch('/worldl.json') | ||
.then((r)=>r.json()) | .then((r)=>r.json()) | ||
.then((r)=>{ | .then((r)=>{ | ||
r. | let tgt = r[184]; | ||
document.body.innerHTML = | |||
`<div>${tgt.population.toLocaleString()}</div>`; | |||
}); | |||
</pre> | </pre> | ||
<pre class=ans> | <pre class=ans> | ||
Line 80: | Line 133: | ||
.then((r)=>r.json()) | .then((r)=>r.json()) | ||
.then((r)=>{ | .then((r)=>{ | ||
document.body.innerHTML = | let tgt = r[184]; | ||
document.body.innerHTML = ` | |||
<table> | |||
<tr><th>Name</th><td>${tgt.name}</td></tr> | |||
<tr><th>Continent</th><td>${tgt.continent}</td></tr> | |||
<tr><th>Capital</th><td>${tgt.capital}</td></tr> | |||
<tr><th>Population</th><td>${tgt.population.toLocaleString('en')}</td></tr> | |||
<tr><th>GDP</th><td>$${(tgt.gdp/1e9).toLocaleString('en')} billion</td></tr> | |||
</table> | |||
`; | |||
}); | |||
</pre> | </pre> | ||
</div> | </div> |
Latest revision as of 21:51, 23 August 2021
Get the name from the number
- You can obtain data from the server using a fetch call.
- In these examples you will be getting data from https://progzoo.net/worldl.json this is a list of 195 countries in json format
- The program snippet shows the name of country 42.
- Change it so it shows the name of country 50.
- See if you can find your country.
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let tgt = r[42]; document.body.innerHTML = tgt.name; });
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let tgt = r[50]; document.body.innerHTML = tgt.name; });
Each country has a bunch of data associated, for example:
area: 330803 capital: "Kuala Lumpur" continent: "Asia" flag: "//upload.wikimedia.org/wikipedia/commons/6/66/Flag_of_Malaysia.svg" gdp: 304726000000 id: 102 name: "Malaysia" population: 30177000 tld: ".my"
Show the population
Alter your code so that the output matches the model answer.
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let tgt = r[102]; document.body.innerHTML = `<div>${tgt.name}</div>`; });
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let tgt = r[102]; document.body.innerHTML = `<div>${tgt.name}, ${tgt.capital}</div>`; });
Include image
Include three div elements and an img for country 151. Use
style='width:100px;'
for the imgfetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let tgt = r[151]; document.body.innerHTML = `<div>${tgt.name}, ${tgt.capital}</div>`; });
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let tgt = r[151]; document.body.innerHTML = ` <div>Name: ${tgt.name}</div> <div>Capital: ${tgt.capital}</div> <div>Population: ${tgt.population}</div> <img src='${tgt.flag}' style='width:100px'/> `; });
Output in a table
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let tgt = r[172]; document.body.innerHTML = `<div>${tgt.name}, ${tgt.capital}</div>`; });
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let tgt = r[172]; document.body.innerHTML = ` <table> <tr><th>Name</th><td>${tgt.name}</td</tr> <tr><th>Continent</th><td>${tgt.continent}</td</tr> <tr><th>Capital</th><td>${tgt.capital}</td</tr> <tr><th>Population</th><td>${tgt.population}</td</tr> </table> `; });
Format numbers
For country 184. Use .toLocaleString() to format numbers better. Show the GDP in billions, include $ symbol as GDP is given in US dollars
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let tgt = r[184]; document.body.innerHTML = `<div>${tgt.population.toLocaleString()}</div>`; });
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let tgt = r[184]; document.body.innerHTML = ` <table> <tr><th>Name</th><td>${tgt.name}</td></tr> <tr><th>Continent</th><td>${tgt.continent}</td></tr> <tr><th>Capital</th><td>${tgt.capital}</td></tr> <tr><th>Population</th><td>${tgt.population.toLocaleString('en')}</td></tr> <tr><th>GDP</th><td>$${(tgt.gdp/1e9).toLocaleString('en')} billion</td></tr> </table> `; });