Skip to content

viewProducts function improvements #52

@mattlub

Description

@mattlub

Beauty/public/main.js

Lines 41 to 68 in 9b9fd91

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions