+380681072861(and telegram) 25onealexalex@gmail.com
used ready html code
the same code from lesson 1
click Edit source(pencil icon), paste the code below into editor field, click Save changes(check mark icon)
1.js - there is the code for Lesson 4 (image gallery from an array of objects). To get and use an array of objects from API (for example http://api.25one.com.ua/olympics24.php) 2.Replace arrayImages with an empty array let arrayImages = []; 3.Fetch-method will be used to get data from API - add a new function getData function getData() { fetch("https://olympics24.25one.com.ua/index.php") .then(responce => { return responce.json(); }) .then(data => { console.log(data); //data - an array of objects from API //...then point 5 }); } 4.Call getData() instead createContent //createContent(arrayImages[0]); getData(); 5.Add data from API to arrayImages(second then in fetch) + call createContent(arrayImages[0]) here ... .then(data => { console.log(data); //data - an array of objects from API arrayImages = data; createContent(arrayImages[0]); });
document.querySelector('.arrow.right').addEventListener('click', () => {
    changeImageRight();      
 });
 
 document.querySelector('.arrow.left').addEventListener('click', () => {
    changeImageLeft();      
 });
 
 let arrayImages = [];
 
 let i = 0;
 
 function getData() {
     fetch("https://olympics24.25one.com.ua/index.php")
     .then(responce => {
         return responce.json();
     })
     .then(data => {
         console.log(data); //data - an array of objects from API
 
         arrayImages = data; 
 
         createContent(arrayImages[0]); 
     }); 
 } 
 
 function createContent(dataStart) {
    let contentContainer = document.querySelector('.wrap');
 
    let elementImg = document.createElement('img');
    elementImg.setAttribute('src', dataStart.file);
    contentContainer.appendChild(elementImg);
 
     let elementDiv = document.createElement('div');
     elementDiv.setAttribute('class', 'title');
     elementDiv.innerText = dataStart.title;
     contentContainer.appendChild(elementDiv);
 }
 
 function changeImageRight() {
   i++;
 
   if (i == arrayImages.length) {
      i = 0;
   }
 
   setData(i);
 }
 
 function changeImageLeft() {
   i--;
 
   if (i == -1) {
      i = arrayImages.length - 1;
   }
 
   setData(i);
 }
 
 function setData(index) {
    document.querySelector('img').setAttribute('src', arrayImages[index].file);
    document.querySelector('.title').innerText = arrayImages[index].title;   
 }
 
 getData();
download the ready code