{
"topic": "MotoGP Racing",
"subject": "Top 10 Rider Statistics",
"source": "Wikipedia",
"sourceUrl": "https://en.wikipedia.org/wiki/List_of_500cc/MotoGP_Motorcycle_World_Champions#Multiple_champions",
"riderStats": [
{
"name": "Giacomo Agostini",
"rank": "1",
"wins": 122,
"starts": 223,
"link": "https://en.wikipedia.org/wiki/Giacomo_Agostini"
}, {
"name": "Valentino Rossi",
"rank": "2",
"wins": 89,
"starts": 369,
"link": "https://en.wikipedia.org/wiki/Valentino_Rossi"
}
]
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<style>
*{
margin: 0 auto;
padding: 0;
text-align: center;
}
table,tr,th,td{
border: 1px solid #808080;
border-collapse: collapse;
}
.app{
margin: 0 auto;
}
#hotel{
margin: 0 auto;
}
#hotelData{
margin: 0 auto;
}
.myBtn{
width: 100px;/*a width value of 100px*/
padding: 10px;
background-color: #ecf2f4;
margin-top: 15px;
}
.myCaption{
font-size: 21px;
font-weight: bold;
padding-top: 30px;
padding-bottom: 10px;
}
th{
background-color: #a8a8a8;
}
th,td{
padding: 10px;
font-size: 20px;
}
/*nth-child() pseudo-class must be applied to shade even table rows (see “Design Resources”)*/
table tr:nth-child(even){
background-color: #e0e0e0;
}
#leftAlign{
text-align: left;
}
</style>
<script>
function loadJSONDoc(url) {
var xmlhttp;
var txt, x, xx, i;
if(window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
txt = "<table border='1'><caption class='myCaption'>MotoGP Racing:Top 10 Rider Statistics</caption ><tr><th>Rank</th><th>Rider Name</th><th>Total Starts</th><th>Total Wins</th><th>Total Losses</th><th>Win Percentage</th></tr>";
x = JSON.parse(xmlhttp.responseText); ;
xx = x.riderStats;
console.log(xx);
for(i = 0; i < xx.length; i++) {
try {
txt = txt + "<td>" + xx[i].rank + "</td>";
} catch(er) {
txt = txt + "<td> </td>";
}
try {
txt = txt + "<td id='leftAlign'><a href='#' target='_blank'>" + xx[i].name + "</a></td>";
} catch(er) {
txt = txt + "<td> </td>";
}
try {
txt = txt + "<td>" + xx[i].starts + "</td>";
} catch(er) {
txt = txt + "<td> </td>";
}
try {
txt = txt + "<td>" + xx[i].wins + "</td>";
} catch(er) {
txt = txt + "<td> </td>";
}
try {
txt = txt + "<td>" + (parseInt(xx[i].starts)-parseInt(xx[i].wins)) + "</td>";
} catch(er) {
txt = txt + "<td> </td>";
}
try {
let s = ((parseFloat(xx[i].wins)/parseFloat(xx[i].starts))*100).toFixed(2);
if(s < 30){
txt = txt + "<td style='background:#ff9999'>" + s + "%</td>";
}else if(s < 50){
txt = txt + "<td style='background:#ffdb99'>" + s + "%</td>";
}else{
txt = txt + "<td style='background:#99cc99'>" + s + "%</td>";
}
} catch(er) {
txt = txt + "<td> </td>";
}
txt = txt + "</tr>";
}
txt = txt + "</table><h3 style='margin-top:10px'>The raw data used for caculating these results has been obtained from <a href=\"#\" target='_blank'>Wikipedia</a></h3>";
document.getElementById('hotelData').innerHTML = txt;
}
}
}
</script>
</head>
<body>
<div class="app">
<div id="hotel">
<h2 style="margin-top: 10px;">Task4:AJAX&JSON</h2>
<button class="myBtn" onclick="loadJSONDoc('A5_TASK3.json')">Start</button>
</div>
<div id="hotelData">
</div>
</div>
</body>
</html>