Advertisement
kstoyanov

2.Bus Schedule

Nov 2nd, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.  
  3.     const departBtn = document.getElementById('depart');
  4.     const arriveBtn = document.getElementById('arrive');
  5.     const span = document.getElementsByClassName('info')[0];
  6.  
  7.     let nextStop = 'depot';
  8.     let currentStop = '';
  9.  
  10.     function depart() {
  11.  
  12.         fetch(`https://judgetests.firebaseio.com/schedule/${nextStop}.json`)
  13.             .then(checkForErrors)
  14.             .then(res => res.json())
  15.             .then(action)
  16.             .catch(errorHandling)
  17.  
  18.         function checkForErrors(res) {
  19.             if (res.ok === false) {
  20.                 throw new Error(`${res.status} - ${res.statusText}`);
  21.             }
  22.             return res;
  23.         }
  24.  
  25.         function errorHandling(err) {
  26.             span.textContent = 'Error';
  27.             departBtn.disabled = true;
  28.             arriveBtn.disabled = true;
  29.             console.error(err);
  30.         }
  31.         function action({ name, next }) {
  32.             nextStop = next;
  33.             currentStop = name;
  34.             span.textContent = `Next stop ${name}`;
  35.             departBtn.disabled = true;
  36.             arriveBtn.disabled = false;
  37.         }
  38.     }
  39.  
  40.     function arrive() {
  41.         span.textContent = `Arriving at ${currentStop}`;
  42.         currentStop = nextStop;
  43.         departBtn.disabled = false;
  44.         arriveBtn.disabled = true;
  45.     }
  46.  
  47.  
  48.     return {
  49.         depart,
  50.         arrive
  51.     };
  52. }
  53.  
  54. let result = solve();
  55.  
  56. module.exports = result;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement