// model let colors = ['red', 'green', 'blue', 'yellow', 'grey'] let isAdding = false; let addColorName; // view updateView(); function updateView() { document.getElementById('app').innerHTML = /*HTML*/` ${createAddColorHtml()}
${createColorsHtml()}
`; } function createAddColorHtml() { if (!isAdding) return ''; return /*HTML*/ ` `; } function addColor() { colors.push(addColorName); isAdding = false; addColorName = ''; updateView(); } function createColorHtml() { let colorsHtml = ''; for (let i = 0; i < colors.lenght; i++) { let color = colors[i]; colorsHtml += /*HTML*/ `
${color}
`; } return colorsHtml; } // Controller function addColor(){ colors.push(addColorName); isAdding = false; addColorName = ''; updateView(); } function deleteColor(index) { colors.splice(index, 1) updateView(); } function startAdd() { isAdding = true; updateView(); } function cancelAdd() { isAdding = false; updateView(); }