-
Notifications
You must be signed in to change notification settings - Fork 0
/
cities-draft.js
76 lines (61 loc) · 2.08 KB
/
cities-draft.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
const fileInput = document.getElementById('myfile')
let citiesRate = []
/////////////function 1///////////////////
const mainReturn = (fileInput.onchange = () => {
const reader = new FileReader()
console.log('selectedFile')
const selectedFile = fileInput.files[0]
console.log(reader.readAsText(selectedFile)) //- undefined
reader.addEventListener('load', (event) => {
console.log('load')
const fileList = event.target.files
console.log('fileList ' + fileList) //- undefined
const result = event.target.result
console.log(result)
citiesRate = result //comment for formatting
.split(/\r\n/)
.filter((a) => /[1-9]/.test(a))
.map((c, i) => ({
city: c.split(',').slice(2, 3)[0],
place: i + 1,
population: c.split(',').slice(-1)[0],
}))
.sort((a, b) => (b.population || 0) - (a.population || 0))
.slice(0, 10)
console.log(citiesRate)
})
const textarea = document.querySelector('.cities-textarea')
textarea.addEventListener('keypress', function (event) {
console.log('keypress')
const text = textarea.value
if (event.key === 'Enter') {
console.log(text)
}
})
// const buttonGetCities = document.querySelector('.button-get-cities')
// buttonGetCities.addEventListener('click', getCities)
// function getCities () {
// console.log('getCities')
// const text = textarea.value
// console.log(text)
// }
//////////function 2//////////////////
let text = ''
document.querySelector('.button-get-cities').addEventListener('click', () => {
console.log('getCities')
text = textarea.value
console.log(text)
citiesRate.forEach((city) => {
if (text.includes(city.city)) {
text = text.replaceAll(
city.city,
`${city.city} (${city.place} місце в ТОП-10 самих великих міст України, населення ${city.population} чоловік)`
)
}
})
console.log(text)
document.querySelector('.text').innerHTML = text
return text
})
})
console.log('mainReturn' + mainReturn)