-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
54 lines (49 loc) · 2.02 KB
/
script.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
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': '002ff11e69msh642d9244f8c51bfp136aa4jsn74d388486591',
'X-RapidAPI-Host': 'weather-by-api-ninjas.p.rapidapi.com'
}
};
// fetch('https://weather-by-api-ninjas.p.rapidapi.com/v1/weather?city=kolkata', options)
// .then(response => response.json())
// .then(response => {var data = response})
// .catch(err => console.error(err));
const arr = document.getElementsByClassName('text-start');
const arr2 = document.getElementsByClassName('fields');
// To print weather parameters for below 3 cities
for(let i=0; i<arr.length; i++){
fetch(`https://weather-by-api-ninjas.p.rapidapi.com/v1/weather?city=${arr[i].innerHTML}`, options)
.then(response => response.json())
.then(response => {
for(let j=0; j<arr2.length; j++){
let kalu = arr2[j].innerHTML
// console.log(kalu);
// console.log(response[kalu]);
document.getElementById(`${arr[i].innerHTML}/${arr2[j].innerHTML}`).innerHTML=`${response[kalu]}`;
// console.log(i)
} })
.catch(err => console.error(err));
}
var spanParameter = document.getElementsByClassName('span-parameter');
//To find weather of the city searched
function addRecord(){
let formElement = document.getElementById('formElement');
let enteredCity = formElement.value;
// formElement.value = "";
console.log(enteredCity);
// console.log(formElement);
document.getElementById("city-line").innerHTML = `<b>${enteredCity}</b> Weather`;
fetch(`https://weather-by-api-ninjas.p.rapidapi.com/v1/weather?city=${enteredCity}`, options)
.then(response => response.json())
.then(response => {
var variables = ["temp", "humidity", "wind_speed"];
for(let i=0; i<spanParameter.length; i++){
let vulu = variables[i]
console.log(spanParameter);
// console.log(response[kalu]);
spanParameter[i].innerHTML=`${response[vulu]}`;
// console.log(i)
} })
.catch(err => console.error(err));
}