-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathindex.js
More file actions
155 lines (131 loc) Β· 4.49 KB
/
index.js
File metadata and controls
155 lines (131 loc) Β· 4.49 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const getLatAndLon = (city) => {
return axios
.get('http://127.0.0.1:5000/location', {
params: {
q: city,
},
})
.then((response) => {
console.log(response.data);
return {
lat: response.data[0].lat,
lon: response.data[0].lon,
};
})
.then((response)=>{
console.log(response);
getWeatherKelvin(response.lat,response.lon)
})
.catch((error) => {
console.log('Error finding the latitude and longitude:', error.response);
});
};
const getWeatherKelvin = (lat, lon) => {
return axios
.get('http://127.0.0.1:5000/weather', {
params: {
lat: lat,
lon: lon,
},
})
.then((response) => {
console.log(response);
return response.data.current.temp;
})
.then((response)=>{
console.log(response);
return Math.floor((response - 273.15) * 9/5 + 32)
})
.then((response)=>{
console.log(response);
state.temp=response
tempContainer.textContent=`${state.temp}`;
})
.catch((error) => {
console.log('Error finding the latitude and longitude:', error.response);
});
};
const saveCity=()=>{
const cityName= document.getElementById('input').value;
state.city=cityName
console.log(state.city);
getLatAndLon(state.city);
getWeatherKelvin(lat,lon);
}
const realTimeButton = document.getElementById('real-time-temp');
realTimeButton.addEventListener('click', saveCity);
// buttons for tempreture up and down
let btnAdd = document.querySelector('#increment');
let btnSubstract = document.querySelector('#decrement')
let tempContainer=document.querySelector('#tempContainer')
const increment = ()=>{state.temp+=1;
tempContainer.textContent=`${state.temp}`;
changeLandscape();
}
const decrement = () =>{state.temp-=1;
tempContainer.textContent=`${state.temp}`;
changeLandscape();
}
btnAdd.addEventListener('click',increment );
btnSubstract.addEventListener('click',decrement );
// Update name of the city
const updateCityName = () => {
let cityName = document.querySelector('#citynam');
let inputName= document.querySelector('#input').value;
state.city = inputName;
cityName.textContent = state.city;
};
const cityNameInput = document.getElementById('input');
cityNameInput.addEventListener('input', updateCityName);
// Reset name of the city
const state={city:'Tehran',
temp:70}
const reset = () => {
const cityName = document.getElementById('input');
cityName.value = 'Tehran';
updateCityName();
};
const resetButton = document.getElementById('reset');
resetButton.addEventListener('click', reset);
// With updated sky change the garden's sky
const changeSky = () => {
let selectedSky = document.getElementById('skys');
let skyOption = selectedSky.options[selectedSky.selectedIndex].text;
let skyContainer=document.getElementById('garden-sky')
let currentSky ='πββββππππβββββπ'
if (skyOption === 'Sunny') {
currentSky = 'βοΈ βοΈ βοΈ βοΈ βοΈ βοΈ';
} else if (skyOption === 'Cloudy') {
currentSky = 'βοΈβοΈ βοΈ βοΈβοΈ βοΈ π€ βοΈ βοΈβοΈ';
} else if (skyOption === 'Rainy') {
currentSky = 'π§πβπ§π§π§βπ§π¦π§π§π§π§';
} else if (skyOption === 'Snowy') {
currentSky = 'π¨βοΈπ¨π¨βοΈβοΈπ¨βοΈπ¨βοΈβοΈπ¨π¨';
}
skyContainer.textContent = currentSky;
};
const changeUpSky = document.getElementById('skys');
changeUpSky.addEventListener('change', changeSky);
// With updated temp change the garden's landscape
const changeLandscape = () => {
temp=state.temp
let tempContainer=document.getElementById('tempContainer')
let landscapeContainer=document.getElementById('ground')
let currentLandscape ='β¨β¨β¨β¨β¨πππππ'
if (temp>80) {
currentLandscape = "π΅__π_π¦_π΅π΅__π_π_π¦";
tempContainer.style.backgroundColor = 'red';
} else if (temp>=70) {
currentLandscape = "πΈπΏπΌ__π·π»πΏ_βοΈπ±_π»π·"
tempContainer.style.backgroundColor = 'orange';
} else if (temp>=60) {
currentLandscape = "πΎπΎ_π_πͺ¨__π€_πΎπΎπΎ_π"
tempContainer.style.backgroundColor = 'yellow';
} else if (temp<=59) {
currentLandscape = "π²π²βοΈπ²βοΈππ²ππ²π²βοΈππ²"
tempContainer.style.backgroundColor = 'green';
}
landscapeContainer.textContent = currentLandscape;
};
const changeGround = document.getElementById('ground');
changeGround.addEventListener('change', changeSky);