nlvo / web-app-from-scratch-1920

Marvel based web app with the latest comic release, series and/or events.
1 stars 0 forks source link

app.js code verbeteren #4

Closed MohamadAlGhorani closed 4 years ago

MohamadAlGhorani commented 4 years ago

ik zie dat je ongeveer dezelfde code gebruikt twee keer.

console.log(jsonData.data.results[1]);
        for (const comic of jsonData.data.results) {
            console.log(comic);
            const section = document.querySelector('.series');
            const article = document.createElement('article');
            const heading = document.createElement('h2');
            const articleImage = document.createElement('img');

            heading.textContent = `${comic.title}`; //template literals
            articleImage.src = `${comic.thumbnail.path}.${comic.thumbnail.extension}`;

            article.appendChild(articleImage);
            article.appendChild(heading);
            section.appendChild(article);
        }

je kunt deze gedeelte in een functie neer zetten en hergebruiken bij elke fetch()

voorbeeld

function renderHtml (parent){} deze functie krijgt een parent mee als parameter zodat je het data kan toevoegen aan verschillende

nlvo commented 4 years ago

Thanks for the tip! And already fixed.