Difference between revisions of "World Factbook"
Jump to navigation
Jump to search
(12 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<pre id='shellbody' data-qtp='fetch'></pre> | <pre id='shellbody' data-qtp='fetch'></pre> | ||
==Get a button for each letter== | ==Get a button for each letter== | ||
* Show a button for each letter that a country can begin with | |||
* You can use a [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set Set] to remove duplicates from a list. | |||
<div class=qu> | <div class=qu> | ||
<pre class=usr> | <pre class=usr> | ||
Line 18: | Line 20: | ||
.then((r)=>r.json()) | .then((r)=>r.json()) | ||
.then((r)=>{ | .then((r)=>{ | ||
let letters = r.map(c=>c.name[0]); | let letters = new Set(r.map(c=>c.name[0])); | ||
for(let a of letters){ | for(let a of letters){ | ||
let b = document.createElement('button'); | let b = document.createElement('button'); | ||
b.innerText = a; | b.innerText = a; | ||
document.body.append(b); | document.body.append(b); | ||
} | |||
}); | |||
</pre> | |||
</div> | |||
==Get a button for each continent== | |||
* Show a button for each continent | |||
<div class=qu> | |||
<pre class=usr> | |||
</pre> | |||
<pre class=ans> | |||
fetch('/worldl.json') | |||
.then((r)=>r.json()) | |||
.then((r)=>{ | |||
let c = [...new Set(r.map(c=>c.continent))]; | |||
c.sort(); | |||
for(let a of c){ | |||
let b = document.createElement('button'); | |||
b.innerText = a; | |||
document.body.append(b); | |||
} | |||
}); | |||
</pre> | |||
</div> | |||
==Pick a random country from Africa== | |||
* Use Math.random() to pick a country at random | |||
* Note that because of the way the scoring works you should not expect to get 100% | |||
The sample code shows a random country beginning with 'A' | |||
<div class=qu> | |||
<pre class=usr> | |||
fetch('/worldl.json') | |||
.then((r)=>r.json()) | |||
.then((r)=>{ | |||
let alist = r.filter(c=>c.name[0]==='A'); | |||
let randomNumber = Math.floor(Math.random()*alist.length); | |||
let randomCountry = alist[randomNumber]; | |||
document.body.innerText = randomCountry.name; | |||
}); | |||
</pre> | |||
<pre class=ans> | |||
fetch('/worldl.json') | |||
.then((r)=>r.json()) | |||
.then((r)=>{ | |||
let africa = r.filter(c=>c.continent==='Africa'); | |||
let randomNumber = Math.floor(Math.random()*africa.length); | |||
let randomCountry = africa[randomNumber]; | |||
document.body.innerText = randomCountry.name; | |||
}); | |||
</pre> | |||
</div> | |||
==Pick four random countries== | |||
* A large country is one with a population of at least 100 million | |||
* Pick four large countries, without repetition | |||
Bangladesh | |||
Brazil | |||
China | |||
India | |||
Indonesia | |||
Japan | |||
Mexico | |||
Nigeria | |||
Pakistan | |||
Russia | |||
United States | |||
*One way to achieve this is to take a copy of the original list then sort it into a random order. | |||
**You can attach a random number ot each country, then sort by that value. | |||
*You can then take the first four elements of the sorted list. | |||
<div class=qu> | |||
<pre class=usr> | |||
fetch('/worldl.json') | |||
.then((r)=>r.json()) | |||
.then((r)=>{ | |||
let alist = r.filter(c=>c.population >= 100000000); | |||
for(let c of alist){ | |||
c.rand = Math.random(); | |||
} | |||
alist.sort((a,b)=>a.rand-b.rand); | |||
for(let i=0;i<alist.length;i++){ | |||
let d = document.createElement('div'); | |||
d.innerText = alist[i].name; | |||
document.body.append(d); | |||
} | |||
}); | |||
</pre> | |||
<pre class=ans> | |||
fetch('/worldl.json') | |||
.then((r)=>r.json()) | |||
.then((r)=>{ | |||
let alist = r.filter(c=>c.population >= 100000000); | |||
for(let c of alist){ | |||
c.rand = Math.random(); | |||
} | |||
alist.sort((a,b)=>a.rand-b.rand); | |||
for(let i=0;i<4;i++){ | |||
let d = document.createElement('div'); | |||
d.innerText = alist[i].name; | |||
document.body.append(d); | |||
} | } | ||
}); | }); | ||
</pre> | </pre> | ||
</div> | </div> |
Latest revision as of 18:07, 28 July 2022
Get a button for each letter
- Show a button for each letter that a country can begin with
- You can use a Set to remove duplicates from a list.
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let letters = r.map(c=>c.name[0]); for(let a of letters){ let b = document.createElement('button'); b.innerText = a; document.body.append(b); } });
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let letters = new Set(r.map(c=>c.name[0])); for(let a of letters){ let b = document.createElement('button'); b.innerText = a; document.body.append(b); } });
Get a button for each continent
- Show a button for each continent
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let c = [...new Set(r.map(c=>c.continent))]; c.sort(); for(let a of c){ let b = document.createElement('button'); b.innerText = a; document.body.append(b); } });
Pick a random country from Africa
- Use Math.random() to pick a country at random
- Note that because of the way the scoring works you should not expect to get 100%
The sample code shows a random country beginning with 'A'
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let alist = r.filter(c=>c.name[0]==='A'); let randomNumber = Math.floor(Math.random()*alist.length); let randomCountry = alist[randomNumber]; document.body.innerText = randomCountry.name; });
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let africa = r.filter(c=>c.continent==='Africa'); let randomNumber = Math.floor(Math.random()*africa.length); let randomCountry = africa[randomNumber]; document.body.innerText = randomCountry.name; });
Pick four random countries
- A large country is one with a population of at least 100 million
- Pick four large countries, without repetition
Bangladesh Brazil China India Indonesia Japan Mexico Nigeria Pakistan Russia United States
- One way to achieve this is to take a copy of the original list then sort it into a random order.
- You can attach a random number ot each country, then sort by that value.
- You can then take the first four elements of the sorted list.
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let alist = r.filter(c=>c.population >= 100000000); for(let c of alist){ c.rand = Math.random(); } alist.sort((a,b)=>a.rand-b.rand); for(let i=0;i<alist.length;i++){ let d = document.createElement('div'); d.innerText = alist[i].name; document.body.append(d); } });
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let alist = r.filter(c=>c.population >= 100000000); for(let c of alist){ c.rand = Math.random(); } alist.sort((a,b)=>a.rand-b.rand); for(let i=0;i<4;i++){ let d = document.createElement('div'); d.innerText = alist[i].name; document.body.append(d); } });