-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (46 loc) · 2.19 KB
/
Copy pathindex.js
File metadata and controls
59 lines (46 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const clickButtonEl = document.getElementById("submit");
function handleClickEvent(event) {
event.preventDefault();
(async () => {
const res = await axios.get("https://api.data.gov.sg/v1/transport/carpark-availability");
console.log("status", res.status);
// Print carpark number
const input = document.getElementById("carparkNumber");
let numberOutput = document.getElementById("numberOutput")
numberOutput.innerText = input.value;
// Convert timestamp into date and time outputs
let timeStamp = res.data.items[0].timestamp;
let dateStamp = timeStamp.slice(0, 10);
let dateOutput = document.getElementById('dateOutput');
dateOutput.innerText = dateStamp;
console.log('Date: ', dateStamp);
let TimeStamp = timeStamp.slice(11, 16);
let timeOutput = document.getElementById('timeOutput');
timeOutput.innerText = TimeStamp;
console.log('Time: ', TimeStamp);
// Extracting selected array
let dataset = res.data.items[0].carpark_data;
let filteredCarpark = dataset.filter(function(dataset) {
if (dataset.carpark_number === input.value) {
return dataset;
};
})
let carparkInfo = filteredCarpark.map(e => {
return e.carpark_info;
})
//Extracting information from selected array and inserting extracted value
let infoString = JSON.stringify(carparkInfo);
let lotsOutput = document.getElementById('lotsOutput');
let totalLots = (infoString.slice(15, 22)).replace(/[^0-9]/g, '');
lotsOutput.innerText = totalLots;
console.log('Total lots: ', totalLots);
let typeOutput = document.getElementById('typeOutput');
let lotType = (infoString.slice(32, 36)).replace(/[^a-zA-Z ]/g, "");
typeOutput.innerText = lotType;
console.log('Lot type: ', lotType);
let availableOutput = document.getElementById('availableOutput');
let lotsAvailable = (infoString.slice(50,60)).replace(/[^0-9]/g, '');
availableOutput.innerText = lotsAvailable;
console.log('Lots available: ', lotsAvailable);
})();
};