dabidabidesh

кориг. домашно (2-ра зад.)

Nov 9th, 2020
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.  
  3.   const url = `https://judgetests.firebaseio.com/schedule/`;  //имаше интервал в края!
  4.   let lastId = 'depot';
  5.   //let lastStop = "";
  6.   let infoField = document.querySelector('#info span');
  7.   let departButton = document.getElementById('depart');
  8.   let arriveButton = document.getElementById('arrive');
  9.  
  10.   function depart() {
  11.  
  12.     let currUrl = `${url}${lastId}.json`;
  13.     fetch(currUrl)
  14.       .then(response => response.json())
  15.       .then(response => infoField.textContent = `Next stop ${response.name}`)
  16.       //lastStop = response.name; //не е нужно
  17.       //lastId = response.next;
  18.       //)
  19.       .catch(() => {
  20.         infoField.textContent = 'Error';
  21.         departButton.disabled = true;
  22.         arriveButton.disabled = true;
  23.       });
  24.  
  25.     departButton.disabled = true;
  26.     arriveButton.disabled = false;
  27.   }
  28.  
  29.   function arrive() {
  30.  
  31.     fetch(`${url}${lastId}.json`)
  32.       .then(response => response.json())
  33.       .then(result => {
  34.         infoField.textContent = `Arriving at ${result.name}`;
  35.         lastId = result.next; //Тук вече трябва да се запази
  36.       })
  37.       .catch(() => {
  38.         infoField.textContent = 'Error';
  39.         departButton.disabled = true;
  40.         arriveButton.disabled = true;
  41.       });
  42.  
  43.     //infoField.textContent = `Arriving at ${lastStop}`;
  44.     arriveButton.disabled = true;
  45.     departButton.disabled = false;
  46.   }
  47.  
  48.   return {
  49.     depart,
  50.     arrive
  51.   };
  52. }
  53.  
  54. let result = solve();
Add Comment
Please, Sign In to add comment