oppgave om arrays

This commit is contained in:
Geir Okkenhaug Jerstad 2024-09-06 14:22:47 +02:00
parent 9350cc3efb
commit c7208b5d99
2 changed files with 112 additions and 0 deletions

38
oppgaver/uke5/array1.html Normal file
View file

@ -0,0 +1,38 @@
<!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>
const app = document.getElementById('app');
const pocketStuff = ['Hat', 'Pencil', ];
view();
function view() {
let html = '';
html += /*HTML*/ `
<button onclick="seeWhatsInPocket()">
See what is in the pockets!
</button>
`;
app.innerHTML = html;
}
// controller
function seeWhatsInPocket(){
for (i = 0; i < pocketStuff.lenght; i++){
console.log(i);
app.innerHTML += /*HTML*/ `
<li>${pocketStuff[i]}</li>
`;
}
}
</script>
</body>
</html>

74
oppgaver/uke5/array2.html Normal file
View file

@ -0,0 +1,74 @@
<!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 = [];
let app = document.getElementById('app');
let tv = 0;
let computer = 0;
let gaming = 0;
let coffee = 0;
// View
updateView();
function updateView(){
let item;
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>
`;
app.innerHTML += htmlCart;
}
</script>
</body>
</html>