-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (58 loc) · 2.31 KB
/
index.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require("babel-core/register");
require("babel-polyfill");
"use strict"
function runScript () {
const location = 'Birmingham'; //turn into function
const api = `1b4d147a771b3fdaab5cbdcb7fe437d4`;
async function destinationResults (url) {
var response = await fetch(url);
return response.json();
}
function runPromise (location) {
location = location.replace(/\s/g, "");
const LOCATIONURL = `http://api.positionstack.com/v1/forward?access_key=${api}&query=${location}`
return (destinationResults(LOCATIONURL).then(({data}) => {
const [first, second, third, ...rest] = data;
const lat = first.latitude;
const lng = first.longitude;
const name = first.label;
const POLICEURL = `https://data.police.uk/api/crimes-street/all-crime?lat=${lat}&lng=${lng}`;
return destinationResults(POLICEURL);
}).then((data) => {
var num = 1
var numberOfCrimes = data.reduce((elm, acc)=> {
var category = acc.category;
if(!elm[acc.category]){
elm[acc.category] = 1
}else{
elm[acc.category] ++
}
return elm
}, {})
return numberOfCrimes
}).then((numberOfCrimes) => {
var container = document.querySelector('.crime-container');
var div = Object.keys(numberOfCrimes).map((elm) => {
var crimeDiv = document.createElement('div');
crimeDiv.classList.add('crime');
var textNode = document.createTextNode(`${elm} ${numberOfCrimes[elm]} `);
crimeDiv.appendChild(textNode)
return (
crimeDiv
)
});
div.forEach(elm => {
container.appendChild(elm)
})
}).catch(() => {
console.log('error occuredx')
}))
}
document.addEventListener('click', (event) => {
if(event.target == document.getElementById('search')){
var inputText = document.getElementById('input').value;
runPromise(inputText);
}
})
}
document.addEventListener('DOMContentLoaded', runScript)