From fc7f07ef4716169a82f30e53eee634523331326f Mon Sep 17 00:00:00 2001 From: Geir Okkenhaug Jerstad Date: Thu, 22 Aug 2024 12:21:23 +0200 Subject: [PATCH] conditionals --- .../04_data/code/chapter/04_data.js | 55 +++++++++ eloquentjavascript/04_data/code/journal.js | 99 +++++++++++++++ eloquentjavascript/04_data/code/load.js | 9 ++ eloquentjavascript/04_data/index.html | 15 +++ eloquentjavascript/04_data/run_with_node.js | 12 ++ eloquentjavascript/data_structures.js | 116 ++++++++++++++++++ eloquentjavascript/my-node-cli/index.js | 13 ++ .../my-node-cli/package-lock.json | 24 ++++ eloquentjavascript/my-node-cli/package.json | 15 +++ intro_js/conditionals.html | 17 +++ kodekos/variabler.html | 44 +++++++ oppgaver/uke3/lyspære.html | 94 ++++++++++++++ oppgaver/uke3/this.html | 5 +- 13 files changed, 516 insertions(+), 2 deletions(-) create mode 100644 eloquentjavascript/04_data/code/chapter/04_data.js create mode 100644 eloquentjavascript/04_data/code/journal.js create mode 100644 eloquentjavascript/04_data/code/load.js create mode 100644 eloquentjavascript/04_data/index.html create mode 100644 eloquentjavascript/04_data/run_with_node.js create mode 100644 eloquentjavascript/data_structures.js create mode 100644 eloquentjavascript/my-node-cli/index.js create mode 100644 eloquentjavascript/my-node-cli/package-lock.json create mode 100644 eloquentjavascript/my-node-cli/package.json create mode 100644 intro_js/conditionals.html create mode 100644 oppgaver/uke3/lyspære.html diff --git a/eloquentjavascript/04_data/code/chapter/04_data.js b/eloquentjavascript/04_data/code/chapter/04_data.js new file mode 100644 index 0000000..f6af2a9 --- /dev/null +++ b/eloquentjavascript/04_data/code/chapter/04_data.js @@ -0,0 +1,55 @@ +var journal = []; + +function addEntry(events, squirrel) { + journal.push({events, squirrel}); +} + +function phi(table) { + return (table[3] * table[0] - table[2] * table[1]) / + Math.sqrt((table[2] + table[3]) * + (table[0] + table[1]) * + (table[1] + table[3]) * + (table[0] + table[2])); +} + +function tableFor(event, journal) { + let table = [0, 0, 0, 0]; + for (let i = 0; i < journal.length; i++) { + let entry = journal[i], index = 0; + if (entry.events.includes(event)) index += 1; + if (entry.squirrel) index += 2; + table[index] += 1; + } + return table; +} + +function journalEvents(journal) { + let events = []; + for (let entry of journal) { + for (let event of entry.events) { + if (!events.includes(event)) { + events.push(event); + } + } + } + return events; +} + +function max(...numbers) { + let result = -Infinity; + for (let number of numbers) { + if (number > result) result = number; + } + return result; +} + +var list = { + value: 1, + rest: { + value: 2, + rest: { + value: 3, + rest: null + } + } +}; diff --git a/eloquentjavascript/04_data/code/journal.js b/eloquentjavascript/04_data/code/journal.js new file mode 100644 index 0000000..ee98d22 --- /dev/null +++ b/eloquentjavascript/04_data/code/journal.js @@ -0,0 +1,99 @@ +var JOURNAL = [ + {"events":["carrot","exercise","weekend"],"squirrel":false}, + {"events":["bread","pudding","brushed teeth","weekend","touched tree"],"squirrel":false}, + {"events":["carrot","nachos","brushed teeth","cycling","weekend"],"squirrel":false}, + {"events":["brussel sprouts","ice cream","brushed teeth","computer","weekend"],"squirrel":false}, + {"events":["potatoes","candy","brushed teeth","exercise","weekend","dentist"],"squirrel":false}, + {"events":["brussel sprouts","pudding","brushed teeth","running","weekend"],"squirrel":false}, + {"events":["pizza","brushed teeth","computer","work","touched tree"],"squirrel":false}, + {"events":["bread","beer","brushed teeth","cycling","work"],"squirrel":false}, + {"events":["cauliflower","brushed teeth","work"],"squirrel":false}, + {"events":["pizza","brushed teeth","cycling","work"],"squirrel":false}, + {"events":["lasagna","nachos","brushed teeth","work"],"squirrel":false}, + {"events":["brushed teeth","weekend","touched tree"],"squirrel":false}, + {"events":["lettuce","brushed teeth","television","weekend"],"squirrel":false}, + {"events":["spaghetti","brushed teeth","work"],"squirrel":false}, + {"events":["brushed teeth","computer","work"],"squirrel":false}, + {"events":["lettuce","nachos","brushed teeth","work"],"squirrel":false}, + {"events":["carrot","brushed teeth","running","work"],"squirrel":false}, + {"events":["brushed teeth","work"],"squirrel":false}, + {"events":["cauliflower","reading","weekend"],"squirrel":false}, + {"events":["bread","brushed teeth","weekend"],"squirrel":false}, + {"events":["lasagna","brushed teeth","exercise","work"],"squirrel":false}, + {"events":["spaghetti","brushed teeth","reading","work"],"squirrel":false}, + {"events":["carrot","ice cream","brushed teeth","television","work"],"squirrel":false}, + {"events":["spaghetti","nachos","work"],"squirrel":false}, + {"events":["cauliflower","ice cream","brushed teeth","cycling","work"],"squirrel":false}, + {"events":["spaghetti","peanuts","computer","weekend"],"squirrel":true}, + {"events":["potatoes","ice cream","brushed teeth","computer","weekend"],"squirrel":false}, + {"events":["potatoes","ice cream","brushed teeth","work"],"squirrel":false}, + {"events":["peanuts","brushed teeth","running","work"],"squirrel":false}, + {"events":["potatoes","exercise","work"],"squirrel":false}, + {"events":["pizza","ice cream","computer","work"],"squirrel":false}, + {"events":["lasagna","ice cream","work"],"squirrel":false}, + {"events":["cauliflower","candy","reading","weekend"],"squirrel":false}, + {"events":["lasagna","nachos","brushed teeth","running","weekend"],"squirrel":false}, + {"events":["potatoes","brushed teeth","work"],"squirrel":false}, + {"events":["carrot","work"],"squirrel":false}, + {"events":["pizza","beer","work","dentist"],"squirrel":false}, + {"events":["lasagna","pudding","cycling","work"],"squirrel":false}, + {"events":["spaghetti","brushed teeth","reading","work"],"squirrel":false}, + {"events":["spaghetti","pudding","television","weekend"],"squirrel":false}, + {"events":["bread","brushed teeth","exercise","weekend"],"squirrel":false}, + {"events":["lasagna","peanuts","work"],"squirrel":true}, + {"events":["pizza","work"],"squirrel":false}, + {"events":["potatoes","exercise","work"],"squirrel":false}, + {"events":["brushed teeth","exercise","work"],"squirrel":false}, + {"events":["spaghetti","brushed teeth","television","work"],"squirrel":false}, + {"events":["pizza","cycling","weekend"],"squirrel":false}, + {"events":["carrot","brushed teeth","weekend"],"squirrel":false}, + {"events":["carrot","beer","brushed teeth","work"],"squirrel":false}, + {"events":["pizza","peanuts","candy","work"],"squirrel":true}, + {"events":["carrot","peanuts","brushed teeth","reading","work"],"squirrel":false}, + {"events":["potatoes","peanuts","brushed teeth","work"],"squirrel":false}, + {"events":["carrot","nachos","brushed teeth","exercise","work"],"squirrel":false}, + {"events":["pizza","peanuts","brushed teeth","television","weekend"],"squirrel":false}, + {"events":["lasagna","brushed teeth","cycling","weekend"],"squirrel":false}, + {"events":["cauliflower","peanuts","brushed teeth","computer","work","touched tree"],"squirrel":false}, + {"events":["lettuce","brushed teeth","television","work"],"squirrel":false}, + {"events":["potatoes","brushed teeth","computer","work"],"squirrel":false}, + {"events":["bread","candy","work"],"squirrel":false}, + {"events":["potatoes","nachos","work"],"squirrel":false}, + {"events":["carrot","pudding","brushed teeth","weekend"],"squirrel":false}, + {"events":["carrot","brushed teeth","exercise","weekend","touched tree"],"squirrel":false}, + {"events":["brussel sprouts","running","work"],"squirrel":false}, + {"events":["brushed teeth","work"],"squirrel":false}, + {"events":["lettuce","brushed teeth","running","work"],"squirrel":false}, + {"events":["candy","brushed teeth","work"],"squirrel":false}, + {"events":["brussel sprouts","brushed teeth","computer","work"],"squirrel":false}, + {"events":["bread","brushed teeth","weekend"],"squirrel":false}, + {"events":["cauliflower","brushed teeth","weekend"],"squirrel":false}, + {"events":["spaghetti","candy","television","work","touched tree"],"squirrel":false}, + {"events":["carrot","pudding","brushed teeth","work"],"squirrel":false}, + {"events":["lettuce","brushed teeth","work"],"squirrel":false}, + {"events":["carrot","ice cream","brushed teeth","cycling","work"],"squirrel":false}, + {"events":["pizza","brushed teeth","work"],"squirrel":false}, + {"events":["spaghetti","peanuts","exercise","weekend"],"squirrel":true}, + {"events":["bread","beer","computer","weekend","touched tree"],"squirrel":false}, + {"events":["brushed teeth","running","work"],"squirrel":false}, + {"events":["lettuce","peanuts","brushed teeth","work","touched tree"],"squirrel":false}, + {"events":["lasagna","brushed teeth","television","work"],"squirrel":false}, + {"events":["cauliflower","brushed teeth","running","work"],"squirrel":false}, + {"events":["carrot","brushed teeth","running","work"],"squirrel":false}, + {"events":["carrot","reading","weekend"],"squirrel":false}, + {"events":["carrot","peanuts","reading","weekend"],"squirrel":true}, + {"events":["potatoes","brushed teeth","running","work"],"squirrel":false}, + {"events":["lasagna","ice cream","work","touched tree"],"squirrel":false}, + {"events":["cauliflower","peanuts","brushed teeth","cycling","work"],"squirrel":false}, + {"events":["pizza","brushed teeth","running","work"],"squirrel":false}, + {"events":["lettuce","brushed teeth","work"],"squirrel":false}, + {"events":["bread","brushed teeth","television","weekend"],"squirrel":false}, + {"events":["cauliflower","peanuts","brushed teeth","weekend"],"squirrel":false} +]; + +// This makes sure the data is exported in node.js — +// `require('./path/to/journal.js')` will get you the array. +if (typeof module != "undefined" && module.exports && (typeof window == "undefined" || window.exports != exports)) + module.exports = JOURNAL; +if (typeof global != "undefined" && !global.JOURNAL) + global.JOURNAL = JOURNAL; diff --git a/eloquentjavascript/04_data/code/load.js b/eloquentjavascript/04_data/code/load.js new file mode 100644 index 0000000..bf44c10 --- /dev/null +++ b/eloquentjavascript/04_data/code/load.js @@ -0,0 +1,9 @@ +// Since the code for most chapter in Eloquent JavaScript isn't +// written with node's module system in mind, this kludge is used to +// load dependency files into the global namespace, so that the +// examples can run on node. + +module.exports = function(...args) { + for (let arg of args) + (1,eval)(require("fs").readFileSync(__dirname + "/../" + arg, "utf8")) +} diff --git a/eloquentjavascript/04_data/index.html b/eloquentjavascript/04_data/index.html new file mode 100644 index 0000000..dcd0b38 --- /dev/null +++ b/eloquentjavascript/04_data/index.html @@ -0,0 +1,15 @@ + + + + + \ No newline at end of file diff --git a/eloquentjavascript/04_data/run_with_node.js b/eloquentjavascript/04_data/run_with_node.js new file mode 100644 index 0000000..88ebd20 --- /dev/null +++ b/eloquentjavascript/04_data/run_with_node.js @@ -0,0 +1,12 @@ +// load dependencies +require("./code/load")("code/journal.js", "code/chapter/04_data.js"); + +for (let event of journalEvents(JOURNAL)) { + let correlation = phi(tableFor(event, JOURNAL)); + if (correlation > 0.1 || correlation < -0.1) { + console.log(event + ":", correlation); + } +} +// → brushed teeth: -0.3805211953 +// → work: -0.1371988681 +// → reading: 0.1106828054 diff --git a/eloquentjavascript/data_structures.js b/eloquentjavascript/data_structures.js new file mode 100644 index 0000000..58e68b3 --- /dev/null +++ b/eloquentjavascript/data_structures.js @@ -0,0 +1,116 @@ +// Weresquirrel + +var JOURNAL = [ + {"events":["carrot","exercise","weekend"],"squirrel":false}, + {"events":["bread","pudding","brushed teeth","weekend","touched tree"],"squirrel":false}, + {"events":["carrot","nachos","brushed teeth","cycling","weekend"],"squirrel":false}, + {"events":["brussel sprouts","ice cream","brushed teeth","computer","weekend"],"squirrel":false}, + {"events":["potatoes","candy","brushed teeth","exercise","weekend","dentist"],"squirrel":false}, + {"events":["brussel sprouts","pudding","brushed teeth","running","weekend"],"squirrel":false}, + {"events":["pizza","brushed teeth","computer","work","touched tree"],"squirrel":false}, + {"events":["bread","beer","brushed teeth","cycling","work"],"squirrel":false}, + {"events":["cauliflower","brushed teeth","work"],"squirrel":false}, + {"events":["pizza","brushed teeth","cycling","work"],"squirrel":false}, + {"events":["lasagna","nachos","brushed teeth","work"],"squirrel":false}, + {"events":["brushed teeth","weekend","touched tree"],"squirrel":false}, + {"events":["lettuce","brushed teeth","television","weekend"],"squirrel":false}, + {"events":["spaghetti","brushed teeth","work"],"squirrel":false}, + {"events":["brushed teeth","computer","work"],"squirrel":false}, + {"events":["lettuce","nachos","brushed teeth","work"],"squirrel":false}, + {"events":["carrot","brushed teeth","running","work"],"squirrel":false}, + {"events":["brushed teeth","work"],"squirrel":false}, + {"events":["cauliflower","reading","weekend"],"squirrel":false}, + {"events":["bread","brushed teeth","weekend"],"squirrel":false}, + {"events":["lasagna","brushed teeth","exercise","work"],"squirrel":false}, + {"events":["spaghetti","brushed teeth","reading","work"],"squirrel":false}, + {"events":["carrot","ice cream","brushed teeth","television","work"],"squirrel":false}, + {"events":["spaghetti","nachos","work"],"squirrel":false}, + {"events":["cauliflower","ice cream","brushed teeth","cycling","work"],"squirrel":false}, + {"events":["spaghetti","peanuts","computer","weekend"],"squirrel":true}, + {"events":["potatoes","ice cream","brushed teeth","computer","weekend"],"squirrel":false}, + {"events":["potatoes","ice cream","brushed teeth","work"],"squirrel":false}, + {"events":["peanuts","brushed teeth","running","work"],"squirrel":false}, + {"events":["potatoes","exercise","work"],"squirrel":false}, + {"events":["pizza","ice cream","computer","work"],"squirrel":false}, + {"events":["lasagna","ice cream","work"],"squirrel":false}, + {"events":["cauliflower","candy","reading","weekend"],"squirrel":false}, + {"events":["lasagna","nachos","brushed teeth","running","weekend"],"squirrel":false}, + {"events":["potatoes","brushed teeth","work"],"squirrel":false}, + {"events":["carrot","work"],"squirrel":false}, + {"events":["pizza","beer","work","dentist"],"squirrel":false}, + {"events":["lasagna","pudding","cycling","work"],"squirrel":false}, + {"events":["spaghetti","brushed teeth","reading","work"],"squirrel":false}, + {"events":["spaghetti","pudding","television","weekend"],"squirrel":false}, + {"events":["bread","brushed teeth","exercise","weekend"],"squirrel":false}, + {"events":["lasagna","peanuts","work"],"squirrel":true}, + {"events":["pizza","work"],"squirrel":false}, + {"events":["potatoes","exercise","work"],"squirrel":false}, + {"events":["brushed teeth","exercise","work"],"squirrel":false}, + {"events":["spaghetti","brushed teeth","television","work"],"squirrel":false}, + {"events":["pizza","cycling","weekend"],"squirrel":false}, + {"events":["carrot","brushed teeth","weekend"],"squirrel":false}, + {"events":["carrot","beer","brushed teeth","work"],"squirrel":false}, + {"events":["pizza","peanuts","candy","work"],"squirrel":true}, + {"events":["carrot","peanuts","brushed teeth","reading","work"],"squirrel":false}, + {"events":["potatoes","peanuts","brushed teeth","work"],"squirrel":false}, + {"events":["carrot","nachos","brushed teeth","exercise","work"],"squirrel":false}, + {"events":["pizza","peanuts","brushed teeth","television","weekend"],"squirrel":false}, + {"events":["lasagna","brushed teeth","cycling","weekend"],"squirrel":false}, + {"events":["cauliflower","peanuts","brushed teeth","computer","work","touched tree"],"squirrel":false}, + {"events":["lettuce","brushed teeth","television","work"],"squirrel":false}, + {"events":["potatoes","brushed teeth","computer","work"],"squirrel":false}, + {"events":["bread","candy","work"],"squirrel":false}, + {"events":["potatoes","nachos","work"],"squirrel":false}, + {"events":["carrot","pudding","brushed teeth","weekend"],"squirrel":false}, + {"events":["carrot","brushed teeth","exercise","weekend","touched tree"],"squirrel":false}, + {"events":["brussel sprouts","running","work"],"squirrel":false}, + {"events":["brushed teeth","work"],"squirrel":false}, + {"events":["lettuce","brushed teeth","running","work"],"squirrel":false}, + {"events":["candy","brushed teeth","work"],"squirrel":false}, + {"events":["brussel sprouts","brushed teeth","computer","work"],"squirrel":false}, + {"events":["bread","brushed teeth","weekend"],"squirrel":false}, + {"events":["cauliflower","brushed teeth","weekend"],"squirrel":false}, + {"events":["spaghetti","candy","television","work","touched tree"],"squirrel":false}, + {"events":["carrot","pudding","brushed teeth","work"],"squirrel":false}, + {"events":["lettuce","brushed teeth","work"],"squirrel":false}, + {"events":["carrot","ice cream","brushed teeth","cycling","work"],"squirrel":false}, + {"events":["pizza","brushed teeth","work"],"squirrel":false}, + {"events":["spaghetti","peanuts","exercise","weekend"],"squirrel":true}, + {"events":["bread","beer","computer","weekend","touched tree"],"squirrel":false}, + {"events":["brushed teeth","running","work"],"squirrel":false}, + {"events":["lettuce","peanuts","brushed teeth","work","touched tree"],"squirrel":false}, + {"events":["lasagna","brushed teeth","television","work"],"squirrel":false}, + {"events":["cauliflower","brushed teeth","running","work"],"squirrel":false}, + {"events":["carrot","brushed teeth","running","work"],"squirrel":false}, + {"events":["carrot","reading","weekend"],"squirrel":false}, + {"events":["carrot","peanuts","reading","weekend"],"squirrel":true}, + {"events":["potatoes","brushed teeth","running","work"],"squirrel":false}, + {"events":["lasagna","ice cream","work","touched tree"],"squirrel":false}, + {"events":["cauliflower","peanuts","brushed teeth","cycling","work"],"squirrel":false}, + {"events":["pizza","brushed teeth","running","work"],"squirrel":false}, + {"events":["lettuce","brushed teeth","work"],"squirrel":false}, + {"events":["bread","brushed teeth","television","weekend"],"squirrel":false}, + {"events":["cauliflower","peanuts","brushed teeth","weekend"],"squirrel":false} + ]; + +function tableFor(event, journal) { + let table = [0,0,0,0]; + for (let i = 0; i < journal.length; i++) { + let entry = journal[i], index = 0; + if (entry.events.includes(event)) index += 1; + if (entry.squirrel) index += 2; + table[index] += 1; + } + return table; + } +console.log(tableFor("pizza", JOURNAL)); + + +function phi(table) { + return (table[3] * table[0] - table[2] * table[1]) / Math.sqrt( + (table[2] + table[3]) * + (table[0] + table[1]) * + (table[1] + table[3]) * + (table[0] + table[2])); +}; +// console.log(phi([76, 9, 4, 1])); diff --git a/eloquentjavascript/my-node-cli/index.js b/eloquentjavascript/my-node-cli/index.js new file mode 100644 index 0000000..a2ff2ba --- /dev/null +++ b/eloquentjavascript/my-node-cli/index.js @@ -0,0 +1,13 @@ +#!/usr/bin/env node + +import { program } from "commander"; + +program + .version("1.0.0") + .description("My Node cli") + .option("-n, --name ", "Add your name") + .action((options) => { + console.log('Hey, ${options.name}!'); + }); + + program.parse(process.argv); \ No newline at end of file diff --git a/eloquentjavascript/my-node-cli/package-lock.json b/eloquentjavascript/my-node-cli/package-lock.json new file mode 100644 index 0000000..7779250 --- /dev/null +++ b/eloquentjavascript/my-node-cli/package-lock.json @@ -0,0 +1,24 @@ +{ + "name": "my-node-cli", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "my-node-cli", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "commander": "^12.1.0" + } + }, + "node_modules/commander": { + "version": "12.1.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-12.1.0.tgz", + "integrity": "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==", + "engines": { + "node": ">=18" + } + } + } +} diff --git a/eloquentjavascript/my-node-cli/package.json b/eloquentjavascript/my-node-cli/package.json new file mode 100644 index 0000000..66c3e8a --- /dev/null +++ b/eloquentjavascript/my-node-cli/package.json @@ -0,0 +1,15 @@ +{ + "name": "my-node-cli", + "version": "1.0.0", + "main": "index.js", + "bin": { + "my-node-cli": "./index.js" + }, + "type": "module", + "author": "", + "license": "ISC", + "description": "", + "dependencies": { + "commander": "^12.1.0" + } +} diff --git a/intro_js/conditionals.html b/intro_js/conditionals.html new file mode 100644 index 0000000..c6dd93c --- /dev/null +++ b/intro_js/conditionals.html @@ -0,0 +1,17 @@ + + + + + + Conditionals + + + + + + + \ No newline at end of file diff --git a/kodekos/variabler.html b/kodekos/variabler.html index e69de29..1b77973 100644 --- a/kodekos/variabler.html +++ b/kodekos/variabler.html @@ -0,0 +1,44 @@ + + + + + + Document + + + +
+ Generet: Hvordan opplevde dere denne workshopen? positiv og negativ feedback! + + Fikk jeg noe ut av dette? + Sosialt - kjent med nye, andre perspektiv innsikter? + NK - personlig utfordring, samarbeid + Ok oppgaver? For lett? For vanskelig? Fjasete? + Lærte du noe? + Annen feedback? + Er det verdt tiden eller hadde du foretrukket å kode alene? +
+ + + \ No newline at end of file diff --git a/oppgaver/uke3/lyspære.html b/oppgaver/uke3/lyspære.html new file mode 100644 index 0000000..a627b9e --- /dev/null +++ b/oppgaver/uke3/lyspære.html @@ -0,0 +1,94 @@ + + + + + + If else else if + + + +
Result:
+
Change hungry Status:
+
I'm hungry
+ + + + \ No newline at end of file diff --git a/oppgaver/uke3/this.html b/oppgaver/uke3/this.html index f907bb3..0716720 100644 --- a/oppgaver/uke3/this.html +++ b/oppgaver/uke3/this.html @@ -4,6 +4,7 @@ Document +