|
function viewProducts(array) { |
|
|
|
array.map(element => { |
|
|
|
var product = document.createElement('div'); |
|
product.setAttribute('class', 'container-div'); |
|
mydiv.appendChild(product); |
|
var brand = document.createElement('h1'); |
|
brand.setAttribute('class', 'div-hd1'); |
|
brand.innerText = element.brand; |
|
var name = document.createElement('h2'); |
|
name.setAttribute('class', 'div-hd2'); |
|
name.innerText = element.name; |
|
var image = document.createElement('IMG'); |
|
image.setAttribute('class', 'product-img'); |
|
image.src = element.image; |
|
image.alt = "product image" |
|
var price = document.createElement('h5'); |
|
price.setAttribute('class', 'div-hd5'); |
|
price.innerText = element.price + element.currency; |
|
product.appendChild(brand); |
|
product.appendChild(name); |
|
product.appendChild(image); |
|
product.appendChild(price); |
|
}); |
|
//clear search box |
|
inp.value = ''; |
|
} |
Nice work splitting up the code into functions, I have a few questions for you regarding this function:
- what would be a more suitable method than
map? Why?
- does the "clear search box" functionality belong inside this function?
- how many times does your function append elements to the DOM? Could you do fewer?
- Could the function be split further? e.g. with a
createProductDOMNode function, like in the todo list project.
Beauty/public/main.js
Lines 41 to 68 in 9b9fd91
Nice work splitting up the code into functions, I have a few questions for you regarding this function:
map? Why?createProductDOMNodefunction, like in the todo list project.