Advertisement
EntropyStarRover

Rest 01.Bus Stop

Nov 8th, 2019
168
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getInfo() {
  2.     let inp = document.getElementById("stopId");
  3.     let n = inp.value;
  4.     let stopName = document.getElementById("stopName")
  5.     let bussesUl = document.getElementsByTagName("ul")[0];
  6.     bussesUl.innerHTML = "";
  7.  
  8.  
  9.     fetch(`https://judgetests.firebaseio.com/businfo/${n}.json`)
  10.         .then(response => {
  11.             if (response.status !== 200) {
  12.                 stopName.textContent = "Error";
  13.                 bussesUl.innerHTML = "";
  14.                 return;
  15.             }
  16.             return response.json()
  17.         })
  18.         .then(function (result) {
  19.  
  20.             let stName = result.name;
  21.             stopName.textContent = stName;
  22.             let busesObj = result.buses;
  23.             let busesArr = Object.entries(busesObj);
  24.  
  25.             busesArr.forEach(b => {
  26.                 let content = `Bus ${b[0]} arrives in ${b[1]} minutes`;
  27.                 let newL = document.createElement("li");
  28.                 newL.textContent = content;
  29.                 bussesUl.appendChild(newL);
  30.             })
  31.  
  32.             inp.value = "";
  33.  
  34.         })
  35.  
  36.         .catch(err => {
  37.             stopName.textContent = "Error";
  38.         })
  39.  
  40. }
Advertisement
RAW Paste Data Copied
Advertisement