Created
September 18, 2020 19:21
-
-
Save arnonate/8ec820b9a43e056e6d3089fa64e3bc6e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Hi there! Thanks for taking on this code test. The requirements are listed below: | |
1. Create a "Foods" class or constructor that will take two arguements: a root element and a data object (foodData). | |
2. Render all of the items in the data object into the DOM with the root element as the parent | |
3. If the user clicks a food item, it should be removed from the list | |
Rules: | |
- Only vanilla JS | |
- Feel free to use Google, Bing, DuckDuckGo to look things up | |
- Time limit: 30 minutes | |
*/ | |
/* DO NOT MODIFY */ | |
const rootElement = document.querySelector(".foods"); | |
const foodData = [ | |
{ | |
id: 1, | |
image: "๐ฎ", | |
name: "taco" | |
}, | |
{ | |
id: 2, | |
image: "๐", | |
name: "burger" | |
}, | |
{ | |
id: 3, | |
image: "๐", | |
name: "eggplant" | |
}, | |
{ | |
id: 4, | |
image: "๐", | |
name: "apple" | |
}, | |
{ | |
id: 5, | |
image: "๐ฅ", | |
name: "pancakes" | |
} | |
]; | |
/* DO NOT MODIFY */ | |
/** YOUR CODE BELOW **/ | |
class Foods { | |
constructor(root, data) { | |
this._root = root; | |
this._data = [...data]; | |
this._displayedData = this._data; | |
} | |
renderList() { | |
this._root.innerHTML = ""; | |
this._root.addEventListener("click", event => { | |
const { target } = event; | |
const key = target.getAttribute("data-key"); | |
this._displayedData = this._displayedData.filter(item => item.id !== key); | |
this.renderList(); | |
}); | |
const fragment = document.createDocumentFragment(); | |
this._displayedData.forEach(item => { | |
fragment.appendChild(this.createFoodItem(item)); | |
}); | |
this._root.appendChild(fragment); | |
} | |
createFoodItem(item) { | |
const itemEl = document.createElement("div"); | |
itemEl.innerText = item.image + " " + item.name; | |
itemEl.setAttribute("data-key", item.id); | |
return itemEl; | |
} | |
} | |
const foods = new Foods(rootElement, foodData); | |
foods.renderList(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment