2024-09-06 14:22:47 +02:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>Document</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="app"></div>
|
|
|
|
<script>
|
|
|
|
// Modell
|
|
|
|
let html = '';
|
|
|
|
let htmlCart = '';
|
|
|
|
const goods = ['TV', 'Computer', 'Gaming Consol', 'Coffee'];
|
|
|
|
let shoppingCart = [];
|
2024-09-06 14:34:17 +02:00
|
|
|
let item;
|
2024-09-06 14:22:47 +02:00
|
|
|
let app = document.getElementById('app');
|
|
|
|
let tv = 0;
|
|
|
|
let computer = 0;
|
|
|
|
let gaming = 0;
|
|
|
|
let coffee = 0;
|
|
|
|
|
|
|
|
// View
|
|
|
|
updateView();
|
|
|
|
function updateView(){
|
|
|
|
for (let i = 0; i < goods.length; i++){
|
|
|
|
item = goods[i];
|
|
|
|
html += /*HTML*/ `
|
|
|
|
<li>${item}<br><button id="${item}" value="Generate" onclick="addToCart('${item}')">Legg i handlevogn</li></button>
|
|
|
|
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
html += /*HTML*/ `
|
|
|
|
<br>
|
|
|
|
<button onclick="showCart()">Vis handlevogn</button>
|
|
|
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
app.innerHTML += html;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Controller
|
|
|
|
function addToCart(item){
|
|
|
|
shoppingCart.push(item);
|
|
|
|
if (item == 'TV'){
|
|
|
|
tv++
|
|
|
|
} else if (item == 'Computer'){
|
|
|
|
computer++
|
|
|
|
} else if (item == 'Gaming Consol'){
|
|
|
|
gaming++
|
|
|
|
} else if (item == 'Coffee'){
|
|
|
|
coffee++
|
|
|
|
} else {
|
|
|
|
alert('No')
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(tv, computer, gaming, coffee)
|
|
|
|
|
|
|
|
}
|
|
|
|
function showCart() {
|
|
|
|
htmlCart = '';
|
|
|
|
htmlCart += /*HTML*/ `
|
|
|
|
<div><li>TV antall = ${tv}</li>
|
|
|
|
<li>Computer antall = ${computer}</li>
|
|
|
|
<li>Gaming antall = ${gaming}</li>
|
|
|
|
<li>Kaffe antall = ${coffee}</li>
|
|
|
|
</div>
|
2024-09-06 14:34:17 +02:00
|
|
|
<button onclick="shopMore()">Kjøp mer!!</button>
|
2024-09-06 14:22:47 +02:00
|
|
|
`;
|
2024-09-06 14:34:17 +02:00
|
|
|
app.innerHTML = htmlCart;
|
|
|
|
}
|
|
|
|
function shopMore(){
|
|
|
|
location.reload();
|
2024-09-06 14:22:47 +02:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|