variables and functions

This commit is contained in:
Geir Okkenhaug Jerstad 2024-08-19 13:36:00 +02:00
parent 20fbf34ec8
commit c8446d5c93
6 changed files with 177 additions and 0 deletions

View file

@ -0,0 +1,3 @@
const square = function(x) {
return x * x;
}

View file

@ -0,0 +1,22 @@
<!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>
<button onclick="start()">start</button>
<div id="triangle"><ul>#</ul></div>
<script>
let triangle = "#";
function start(){
triangle += "#"
document.getElementById('triangle').innerHTML += `<ul>${triangle}</ul>`
if (document.getElementById('triangle').innerHTML.length() == 5) {
console.log('Nice');
}
}
</script>
</body>
</html>

View file

@ -0,0 +1,89 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bytting av bakgrunnsfarge på nettleser</title>
<style>
div.bg {
width: 100px;
height: 20px;
border: solid black 1px;
}
.page {
padding: 0;
}
.green-bg {
background: green;
}
.red-bg {
background: red;
}
.blue-bg {
background: blue;
}
.white-text {
color: white;
}
</style>
</head>
<body>
<div id="page">
<h2 id="title">Class List</h2>
<div id="farge1" class="green-bg" onclick="getColor1()">farge1</div>
<div id="farge2" class="blue-bg" onclick="getColor2()">farge2</div>
<div id="farge3" class="red-bg" onclick="getcolor3()">farge3</div>
<button onclick="changeBgColor()">Forandre hele bakgrunnen</button>
<div id="box-1" class="bg" onclick="setColor1()"></div>
<div id="box-2" class="bg" onclick="setColor2()"></div>
</div>
<script>
//let bgColor = document.getElementsByClassName();
var farge;
console.log(farge1, farge2, farge3);
function getColor1() {
farge = document.getElementById('farge1').className;
}
function getColor2() {
farge = document.getElementById('farge2').className;
console.log(farge)
}
function getcolor3() {
farge = document.getElementById('farge3').className;
console.log(farge)
}
function setColor1() {
document.getElementById('box-1').classList.add(farge);
console.log(farge)
}
function setColor2() {
document.getElementById('box-2').classList.add(farge);
console.log(farge)
}
function changeBgColor() {
document.getElementById('page').classList.add(farge);
}
// paragraph.classList.add('bold', 'red-bg', 'big-font');
// paragraph.classList.remove('bold');
// paragraph.classList.replace('big-font', 'white-text');
// paragraph.classList.toggle('red-bg');
// paragraph.classList.toggle('red-bg');
// console.log(paragraph.classList.contains('red-bg'));
// console.log(paragraph.classList);
</script>
</body>
</html>

View file

@ -0,0 +1,39 @@
<!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="page">
<div id="header">Stoppeklokke</div>
<div id="buttons">
<button onclick="myTimer()">Start</button>
<button onclick="stopTimer()">Stopp</button>
<button onclick="resetTimer()">Rundetid</button>
</div>
<div id="time"></div><br>
<div id="round"></div><br>
</div>
<script>
let time = 1;
function myTimer() {
setInterval(counter,1000)
}
function counter() {
document.getElementById('time').innerHTML = time;
++time;
}
function resetTimer() {
document.getElementById('round').innerHTML += `<li>${time -1} sekunder</li`;
clearInterval(time)
time = 0;
}
function stopTimer(){
location.reload();
}
</script>
</body>
</html>

View file

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Let & var scopes</title>
</head>
<body>
<script>
function testScopes() {
var hello = "Var hello";
let hello2 = "Let Hello";
console.log(hello,hello2);
{
var yo = "Var yo i obj";
let yo2 = "Let yo i obj";
console.log(yo, yo2);
}
console.log(yo);
}
testScopes();
</script>
</body>
</html>