good
async function weatherStation(keyword) {
let url = `https://jsonmock.hackerrank.com/api/weather/search?name=${keyword}`;
keyword = keyword.toLowerCase();
let ans = [];
let response = await fetch(url + `&page=1`);
response = await response.json();
let arr = response.data;
//console.log(arr);
for (let j = 0; j < arr.length; j++) {
let obj = arr[j];
let name = obj.name;
name = name.toLowerCase();
if (name.includes(keyword)) {
ans.push(obj);
}
}
let total_pages = response.total_pages;
//console.log(total_pages);
for (let i = 2; i <= total_pages; i++) {
let resp = await fetch(url + `&page=${i}`);
resp = await resp.json();
arr = resp.data;
//console.log(arr);
for (let j = 0; j < arr.length; j++) {
let obj = arr[j];
let name = obj.name;
name = name.toLowerCase();
if (name.includes(keyword)) {
ans.push(obj);
}
}
}
let res = [];
for (let i = 0; i < ans.length; i++) {
let a = String(ans[i].name);
let b = String(ans[i].weather);
let c = String(ans[i].status[0]);
let d = String(ans[i].status[1]);
let x = a + "," + b + "," + c + "," + d;
res.push(x);
}
return res;
}
weatherStation("Adelaide").then((data) => {
console.log(data);
});
Is it correct?
googd