var makeElement = function(tegName, className, text) {
var element = document.createElement(tegName);
element.classList.add(className);
if(text) {
element.textContent = text;
}
return element;
}
var makeCard = function(data) {
let info = data;
let card = makeElement(‘li’, ‘good’);
if (info.inStock) {
card.classList.add('good--available');
} else {
card.classList.add(‘good–unavailable’);
}
let title = makeElement('h2', 'good__description', info.text);
card.appendChild(title);
let picture = makeElement('img', 'good__image');
picture.src = info.imgUrl;
picture.alt = info.text;
card.appendChild(picture);
let price = makeElement('p', 'good__price', info.price + '₽/кг');
card.appendChild(price);
if(info.isHit) {
card.classList.add('good--hit');
let specialOffer = makeElement('p', 'good__special-offer', info.specialOffer);
card.appendChild(specialOffer);
}
return card;
}
var renderCards = function(data) {
var cards = document.querySelector(‘goods’);
for (let i = 0; i < data.length; i++) {
let card = makeCard(data[i]);
cards.appendChild(card);
}
}
renderCards(cardsData);