From 93694b1a5e6c8b8676328f4eeed2d178e641cbe5 Mon Sep 17 00:00:00 2001 From: Rasmus Jones Date: Sun, 21 Apr 2024 16:09:48 +0200 Subject: [PATCH 1/4] inclass files week1 js1 team28 --- javascript1/week1/inclass/ex1.js | 21 ++++++++++ javascript1/week1/inclass/ex2.js | 28 +++++++++++++ javascript1/week1/inclass/ex3.js | 9 ++++ javascript1/week1/inclass/inclass1.html | 9 ++++ javascript1/week1/inclass/inclass1.js | 1 + javascript1/week1/inclass/inclass2.js | 8 ++++ javascript1/week1/inclass/inclass3.js | 19 +++++++++ javascript1/week1/inclass/inclass4.js | 42 +++++++++++++++++++ javascript1/week1/inclass/inclass5.js | 2 + .../week1/inclass/pizza-exercise/index.html | 9 ++++ .../week1/inclass/pizza-exercise/pizza.js | 1 + 11 files changed, 149 insertions(+) create mode 100644 javascript1/week1/inclass/ex1.js create mode 100644 javascript1/week1/inclass/ex2.js create mode 100644 javascript1/week1/inclass/ex3.js create mode 100644 javascript1/week1/inclass/inclass1.html create mode 100644 javascript1/week1/inclass/inclass1.js create mode 100644 javascript1/week1/inclass/inclass2.js create mode 100644 javascript1/week1/inclass/inclass3.js create mode 100644 javascript1/week1/inclass/inclass4.js create mode 100644 javascript1/week1/inclass/inclass5.js create mode 100644 javascript1/week1/inclass/pizza-exercise/index.html create mode 100644 javascript1/week1/inclass/pizza-exercise/pizza.js diff --git a/javascript1/week1/inclass/ex1.js b/javascript1/week1/inclass/ex1.js new file mode 100644 index 00000000..30bf620f --- /dev/null +++ b/javascript1/week1/inclass/ex1.js @@ -0,0 +1,21 @@ +// Types +console.log(typeof 3); +console.log(typeof -33); +console.log(typeof "3"); + +const threeConst = 3; +console.log(threeConst); + +let threeLet = 3; +console.log(threeLet); + +console.log(typeof 'console.log("console.log(console.log(""))")'); + +console.log(typeof typeof 45); + +const names = ["Peter", "Christopher"]; +console.log(typeof names[0]); + +console.log(typeof [3, 10][0]); + +console.log(typeof true); diff --git a/javascript1/week1/inclass/ex2.js b/javascript1/week1/inclass/ex2.js new file mode 100644 index 00000000..72ad18dc --- /dev/null +++ b/javascript1/week1/inclass/ex2.js @@ -0,0 +1,28 @@ +// Create a variable that is 24 times 55 +const thisIsNotAGoodName = 24 * 55; +console.log(thisIsNotAGoodName); + +// Create a const and set it to be equal to your name +const myName = "Peter"; + +// With javascript console.log the first character in your name +console.log(myName[0]); + +// Create an array with 3 strings, three numbers and three booleans +const neitherAGoodName = [ + "Banana", + "Apple", + "Orange", + 42, + 1337, + 0, + true, + false, + true, +]; + +// console.log the 4. element in the array made above +console.log(neitherAGoodName[3]); + +// Optional with javascript console.log the last character in your name. +console.log(myName[myName.length - 1]); diff --git a/javascript1/week1/inclass/ex3.js b/javascript1/week1/inclass/ex3.js new file mode 100644 index 00000000..98117fd5 --- /dev/null +++ b/javascript1/week1/inclass/ex3.js @@ -0,0 +1,9 @@ +const name = "benjamin"; +name = "benjamin-better"; + +const pizzaPrice = 78; +const pizzaPriceDiscounted = pizzaprice - 10; + +const users = ["peter", "Johnny", "Bรธrge"]; + +const lastUser = users[3]; diff --git a/javascript1/week1/inclass/inclass1.html b/javascript1/week1/inclass/inclass1.html new file mode 100644 index 00000000..557e3368 --- /dev/null +++ b/javascript1/week1/inclass/inclass1.html @@ -0,0 +1,9 @@ + + + + Codestin Search App + + + + + diff --git a/javascript1/week1/inclass/inclass1.js b/javascript1/week1/inclass/inclass1.js new file mode 100644 index 00000000..5de14c13 --- /dev/null +++ b/javascript1/week1/inclass/inclass1.js @@ -0,0 +1 @@ +console.log("Hello Rasmus!"); diff --git a/javascript1/week1/inclass/inclass2.js b/javascript1/week1/inclass/inclass2.js new file mode 100644 index 00000000..ce045e8d --- /dev/null +++ b/javascript1/week1/inclass/inclass2.js @@ -0,0 +1,8 @@ +const myName = "Rasmus!"; +let myAge = 33; +console.log(myAge); +myAge = 34; +console.log(myAge); + +// var myShoesize = 44; +// myShoesize = 45; diff --git a/javascript1/week1/inclass/inclass3.js b/javascript1/week1/inclass/inclass3.js new file mode 100644 index 00000000..bbe25c57 --- /dev/null +++ b/javascript1/week1/inclass/inclass3.js @@ -0,0 +1,19 @@ +const myName = "Peter!"; +const myAge = 28; +console.log(typeof myName); +console.log(typeof myAge); + +const myFavouriteFoods = ["Falafel", "Pizza"]; +console.log(myFavouriteFoods); +console.log(myFavouriteFoods[1]); +console.log(typeof myFavouriteFoods); +console.log(typeof myFavouriteFoods[1]); + +console.log(myFavouriteFoods.length); +console.log(myName[myName.length - 1]); + +let mySon; +console.log(mySon); + +const myChildren = []; +console.log(myChildren); diff --git a/javascript1/week1/inclass/inclass4.js b/javascript1/week1/inclass/inclass4.js new file mode 100644 index 00000000..33adaa01 --- /dev/null +++ b/javascript1/week1/inclass/inclass4.js @@ -0,0 +1,42 @@ +// operators +// Assignment operator +const thisIsNotAGoodVariableName = 10; + +// Equality operators +// == equal +const applesBananas = "Apples" == "Bananas"; +console.log(applesBananas); +console.log(typeof applesBananas); + +// != not equal +const notApplesBananas = "Apples" != "Bananas"; +console.log(notApplesBananas); +console.log(typeof notApplesBananas); + +// === strict equal with numbers +const numberEqual = "1" === 1; +console.log(numberEqual); // this will be false +console.log(typeof numberEqual); + +// !== strict not equal with numbers +const numberNotEqual = "2" !== 2; +console.log(numberNotEqual); // this will be false +console.log(typeof numberNotEqual); + +// <, >, <=, >= +// Do the table exercise +console.log(4 <= 4); +console.log("Hello" < "World"); // DONT DO THIS + +// arithmetic operators +console.log(5 - 2); +console.log("Hello" + "World"); + +// modulo +// 55 apples, and 7 kids +// all apples are distributed between the kids +// without any kid being prioritized +// then every kid gets 7 apples with 6 +// apples not being given to anyone +// 6 is the remainder or modulo +console.log(55 % 7); diff --git a/javascript1/week1/inclass/inclass5.js b/javascript1/week1/inclass/inclass5.js new file mode 100644 index 00000000..e3827a81 --- /dev/null +++ b/javascript1/week1/inclass/inclass5.js @@ -0,0 +1,2 @@ +const pizzaName = "Pizza Salami"; +console.log("New Pizza order: " + pizzaName); diff --git a/javascript1/week1/inclass/pizza-exercise/index.html b/javascript1/week1/inclass/pizza-exercise/index.html new file mode 100644 index 00000000..9e4379cb --- /dev/null +++ b/javascript1/week1/inclass/pizza-exercise/index.html @@ -0,0 +1,9 @@ + + + + Codestin Search App + + + + + diff --git a/javascript1/week1/inclass/pizza-exercise/pizza.js b/javascript1/week1/inclass/pizza-exercise/pizza.js new file mode 100644 index 00000000..9660fcc6 --- /dev/null +++ b/javascript1/week1/inclass/pizza-exercise/pizza.js @@ -0,0 +1 @@ +console.log("I love pizza"); From a1860fbd2398e31d77c6d7894fb08016c0f3bb3b Mon Sep 17 00:00:00 2001 From: Rasmus Jones Date: Sun, 28 Apr 2024 18:22:25 +0200 Subject: [PATCH 2/4] inclass files js1 week2 --- .../week2/inclass/.vscode/extensions.json | 6 + .../week2/inclass/.vscode/settings.json | 4 + .../week2/inclass/conditions-exercise.js | 21 ++++ javascript1/week2/inclass/conditions.js | 82 +++++++++++++ .../inclass/functions-exercise-answer.js | 13 ++ .../week2/inclass/functions-exercise.js | 5 + javascript1/week2/inclass/functions.js | 115 ++++++++++++++++++ javascript1/week2/inclass/hw_inclass_dog.js | 14 +++ javascript1/week2/inclass/hw_inclass_names.js | 36 ++++++ javascript1/week2/inclass/rj_exercise1.js | 18 +++ javascript1/week2/inclass/rj_exercise2.js | 3 + javascript1/week2/inclass/rj_exercise3.js | 7 ++ javascript1/week2/inclass/rj_exercise4.js | 20 +++ javascript1/week2/inclass/rj_inclass1.js | 27 ++++ javascript1/week2/inclass/rj_inclass2.js | 18 +++ javascript1/week2/inclass/rj_inclass3.js | 7 ++ 16 files changed, 396 insertions(+) create mode 100644 javascript1/week2/inclass/.vscode/extensions.json create mode 100644 javascript1/week2/inclass/.vscode/settings.json create mode 100644 javascript1/week2/inclass/conditions-exercise.js create mode 100644 javascript1/week2/inclass/conditions.js create mode 100644 javascript1/week2/inclass/functions-exercise-answer.js create mode 100644 javascript1/week2/inclass/functions-exercise.js create mode 100644 javascript1/week2/inclass/functions.js create mode 100644 javascript1/week2/inclass/hw_inclass_dog.js create mode 100644 javascript1/week2/inclass/hw_inclass_names.js create mode 100644 javascript1/week2/inclass/rj_exercise1.js create mode 100644 javascript1/week2/inclass/rj_exercise2.js create mode 100644 javascript1/week2/inclass/rj_exercise3.js create mode 100644 javascript1/week2/inclass/rj_exercise4.js create mode 100644 javascript1/week2/inclass/rj_inclass1.js create mode 100644 javascript1/week2/inclass/rj_inclass2.js create mode 100644 javascript1/week2/inclass/rj_inclass3.js diff --git a/javascript1/week2/inclass/.vscode/extensions.json b/javascript1/week2/inclass/.vscode/extensions.json new file mode 100644 index 00000000..7648e45a --- /dev/null +++ b/javascript1/week2/inclass/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + "recommendations": [ + "esbenp.prettier-vscode", // Prettier extension + "ritwickdey.LiveServer" // Live Server extension + ] +} diff --git a/javascript1/week2/inclass/.vscode/settings.json b/javascript1/week2/inclass/.vscode/settings.json new file mode 100644 index 00000000..9bf4d12b --- /dev/null +++ b/javascript1/week2/inclass/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "editor.defaultFormatter": "esbenp.prettier-vscode", + "editor.formatOnSave": true +} diff --git a/javascript1/week2/inclass/conditions-exercise.js b/javascript1/week2/inclass/conditions-exercise.js new file mode 100644 index 00000000..e50ceb66 --- /dev/null +++ b/javascript1/week2/inclass/conditions-exercise.js @@ -0,0 +1,21 @@ +// Todo: Create an if sentence that will give a user a message based on his bank account balance. Use the balance variable and change that. + +const balance = NaN; +if (balance <= 0) { + // If a user has 0 or less balance log out 'Please deposit some money!' + console.log("Please deposit some money!"); + // If a user has more than 0 and at most 1000 log out 'Your balance is looking okay' +} else if (balance <= 1000) { + console.log("Your balance is looking okay"); + // If a user has more than 1000 and at most 3000 log out 'Your balance is looking good' +} else if (balance <= 3000) { + console.log("Your balance is looking good"); + // If a user has more than 3000 and at most 10000 log out 'Your balance is fantastic' +} else if (balance <= 10000) { + console.log("Your balance is fantastic"); + // If a user has more than 10000 log out 'Your balance is AMAZING!' +} else if (10000 < balance) { + console.log("Your balance is AMAZING!"); +} else { + console.log("Sorry we cant help you"); +} diff --git a/javascript1/week2/inclass/conditions.js b/javascript1/week2/inclass/conditions.js new file mode 100644 index 00000000..9350e465 --- /dev/null +++ b/javascript1/week2/inclass/conditions.js @@ -0,0 +1,82 @@ +// VSCode Extensions: Better Comments & nodemon +// * nodemon = like `node filename.js` but watches for changes on save +// ! Important - fix this +// Todo: Remember to.. +// * Highlight +// ? Question + +// * Can be useful when writing *pseudo code* +// ? Pseudeo code? +// Can easily get confused in the semantics (especially when it comes to conditional logic) +// Helps give you an overview => then write code + +// declare age +// if age is above 18 => Allowed +// else => Denied + +const age = 1; + +// if-else statement, `age > 18` is called an expression +// `age > 18` is called a boolean expression +if (age > 18) { + console.log("Allowed"); +} else { + console.log("Denied"); +} + +// if (age < 18) console.log("You're not old enough"); + +// if (true && true && true){ + +// } +// if (true && true && false){ + +// } + +// Boolean operators +// && is called AND +// || is called OR +// ! is called NOT + +// ? What will be logged if the age = 1? + +// Chained if statement +if (age > 1 && age < 13) { + console.log("You are a child"); +} else if (age >= 13 && age < 20) { + console.log("You are a teenager"); +} else if (age <= 1) { + console.log("You are an infant"); +} else { + console.log("You are an adult"); +} + +if (age < 18 || age === 18) { + // same as saying `age <= 18` + // console.log("You are almost there"); +} + +// New example +const userRole = "guest"; //string +const isAdmin = userRole === "admin"; //boolean +const isLoggedIn = userRole !== "guest"; //boolean + +if (isAdmin) { + console.log("You are in charge"); +} else if (isLoggedIn) { + console.log("Welcome user"); +} else { + console.log("Welcome guest"); +} + +// Tenary operator +//true ? "this code" : "else run this code" +// console.log( +// age > 1 && age < 13 +// ? "You are a child" +// : age >= 13 && age < 20 +// ? "You are a teenager" +// : age <= 1 +// ? "You are an infant" +// : "You are an adult" +// ); diff --git a/javascript1/week2/inclass/functions-exercise-answer.js b/javascript1/week2/inclass/functions-exercise-answer.js new file mode 100644 index 00000000..355176c8 --- /dev/null +++ b/javascript1/week2/inclass/functions-exercise-answer.js @@ -0,0 +1,13 @@ +// BMDAS + +function celciusToFahrenheit(celcius) { + return (celcius * 9) / 5 + 32; +} + +function fahrenheitToCelcius(fahrenheit) { + return ((fahrenheit - 32) * 5) / 9; +} + +// Example usage: +console.log(celciusToFahrenheit(17)); +console.log(fahrenheitToCelcius(62.6)); diff --git a/javascript1/week2/inclass/functions-exercise.js b/javascript1/week2/inclass/functions-exercise.js new file mode 100644 index 00000000..68cdc590 --- /dev/null +++ b/javascript1/week2/inclass/functions-exercise.js @@ -0,0 +1,5 @@ +// Todo: Create a function called getCircleArea. It should have the radius of the circle as parameter and return the circle area. What happens if we dont return anything in the function? + +// Create a function called celciusToFahreneit it should have a parameter called celcius. It should return the temperature in fahrenheit. + +// Try call the function and check with google if the function returns the right value. diff --git a/javascript1/week2/inclass/functions.js b/javascript1/week2/inclass/functions.js new file mode 100644 index 00000000..36b84999 --- /dev/null +++ b/javascript1/week2/inclass/functions.js @@ -0,0 +1,115 @@ +// VSCode Extensions: Better Comments & nodemon +// * nodemon = like `node filename.js` but watches for changes on save +// ! Important - fix this +// Todo: Remember to.. +// * Highlight +// ? Question + +// WHAT ARE FUNCTIONS? +// A function is a reusable piece of code +// console.log() is a great example of this + +// FUNCTION DEFINITION + +// ๐Ÿ‘‡ keyword ๐Ÿ‘‡ function name ๐Ÿ‘‡ parameters ๐Ÿ‘‡ Scope start +function goodFunctionName(parameter1, parameter2) { + // Function body + const total = parameter1 + parameter2; + + // ๐Ÿ‘‡ return value of function (result of the function's calculations) + return total; + // โš ๏ธ Any code after the return function won't run +} +// โ˜๏ธ Scope end + +// Calling the Function +// console.log(goodFunctionName(1, 2)); + +// usage +// const randomNumber = Math.random(); +// console.log(randomNumber); // logs out some number between 0 and 1 + +// const maxNumber = Math.max(3, 5); // 3 and 5 are arguments +// console.log(maxNumber); // logs out 5 + +// Creation +let todoItem = "Buy Groceries"; +// console.log(todoItem); + +// Create the function +function checkItem() { + todoItem = todoItem + " - done"; +} + +// ? What happens now when I save? + +// use the function by calling it using () +// just like console.log() + +// ! Nothing unless we call then function using () +// checkItem(); +// console.log(todoItem); + +// a and b are called parameters. This function has two parameters +function sum(a, b) { + return a + b; +} + +// 5 and 10 are called arguments now when called in a function +// Arguments are "passed" to the function: "we pass `4` and `5` to the function sum +// const returnedSum = sum(5, 10); +// console.log(returnedSum); + +// function multiply(a, b) { +// // the value of a will be substituted with the value of 10! +// // the value of b will be substituted with the value of 4! +// console.log(a, b); // logs out 10 4 +// return a * b; +// } + +// console.log(multiply(10, 4)); + +// Return value +// function sum(a, b) { +// return a + b; +// } +// const returnedSum = sum(5, 10); // the variable returnedSum captures the return value from calling the function! +// console.log(returnedSum); // logs 15 + +// If we dont return, we cannot capture the returned value! +// function sumNoReturn(a, b) { +// a + b; // no return! +// } +// const returnedSum = sum(5, 10); +// console.log(returnedSum); // logs undefined + +// Calling a method on something +// let s = " this is a string "; +// calling a function (trim) on the string s +// s.trim(); // -> "this is a string" + +// ? Bonus Question #1: +// What does `name = โ€œWorldโ€` mean in the following statement? + +// function sayHello(greeting, name = "World") { +// return `${greeting} ${name}!`; +// } + +// console.log(sayHello("Howdy", "Partner")); +// console.log(sayHello("Howdy")); + +// ? Bonus Question #2: +// What's the difference between parameter and argument? + +// function addNumbers(a, b) { // 'a' and 'b' are parameters (when declaring the function) +// return a + b; +// } + +// addNumbers(5, 3); // 5 and 3 are arguments + +// * Out of "scope" for this session but you may come across arrow functions when Googling +const addNumbers = (a, b) => a + b; + +function addNumbers(a, b) { + return a + b; +} diff --git a/javascript1/week2/inclass/hw_inclass_dog.js b/javascript1/week2/inclass/hw_inclass_dog.js new file mode 100644 index 00000000..8751b011 --- /dev/null +++ b/javascript1/week2/inclass/hw_inclass_dog.js @@ -0,0 +1,14 @@ +const dogYearOfBirth = 2017; +const dogYearFuture = 2027; +const shouldShowResultInDogYears = false; + +let dogYear = dogYearFuture - dogYearOfBirth; +let yearsPrefix = "human"; + +if (shouldShowResultInDogYears) { + dogYear *= 7; + yearsPrefix = "dog"; +} +const ouput = `Your dog will be ${dogYear} ${yearsPrefix} years old in ${dogYearFuture}.`; + +console.log(ouput); diff --git a/javascript1/week2/inclass/hw_inclass_names.js b/javascript1/week2/inclass/hw_inclass_names.js new file mode 100644 index 00000000..f10d1791 --- /dev/null +++ b/javascript1/week2/inclass/hw_inclass_names.js @@ -0,0 +1,36 @@ +const firstWords = [ + "Efficient", + "Modern", + "Strategic", + "Reliable", + "Advanced", + "Proactive", + "Agile", + "NextGen", + "Peak", + "Vivid", +]; +const secondWords = [ + "Enterprises", + "Products", + "Ideas", + "Operations", + "Initiatives", + "Engines", + "Platforms", + "Structures", + "Connections", + "Models", +]; + +const firstRandomNumber = Math.floor(Math.random() * 10); +const secondRandomNumber = Math.floor(Math.random() * 10); + +const randomFirstWord = firstWords[firstRandomNumber]; +const randomSecondWord = secondWords[secondRandomNumber]; + +const charactersLength = randomFirstWord.length + randomSecondWord.length; +const startupName = `${randomFirstWord} ${randomSecondWord}`; +const startupMessage = `The startup: "${startupName}" contains ${charactersLength} characters.`; + +console.log(startupMessage); diff --git a/javascript1/week2/inclass/rj_exercise1.js b/javascript1/week2/inclass/rj_exercise1.js new file mode 100644 index 00000000..70d3dd05 --- /dev/null +++ b/javascript1/week2/inclass/rj_exercise1.js @@ -0,0 +1,18 @@ +// With pen and paper write what is logged out. +const global = "global"; +function scopeTest() { + // console.log(functionScope); // undefined ? error + console.log(global); // global + const functionScope = "functionScope"; + + function tester() { + console.log(global); // global + + const testerVariable = "testerVariable"; + } + + tester(); + // console.log(testerVariable); // undefined ? error +} + +scopeTest(); diff --git a/javascript1/week2/inclass/rj_exercise2.js b/javascript1/week2/inclass/rj_exercise2.js new file mode 100644 index 00000000..1c559f0e --- /dev/null +++ b/javascript1/week2/inclass/rj_exercise2.js @@ -0,0 +1,3 @@ +for (let i = 74; i <= 98; i++) { + console.log(i); +} diff --git a/javascript1/week2/inclass/rj_exercise3.js b/javascript1/week2/inclass/rj_exercise3.js new file mode 100644 index 00000000..f45b00ae --- /dev/null +++ b/javascript1/week2/inclass/rj_exercise3.js @@ -0,0 +1,7 @@ +function logString(stringToLog, numberOfTimesToLog) { + for (let i = 0; i < numberOfTimesToLog; i++) { + console.log(stringToLog); + } +} + +logString("not transparent", 5); diff --git a/javascript1/week2/inclass/rj_exercise4.js b/javascript1/week2/inclass/rj_exercise4.js new file mode 100644 index 00000000..9cf5bd39 --- /dev/null +++ b/javascript1/week2/inclass/rj_exercise4.js @@ -0,0 +1,20 @@ +// This function emulates sending emails to receipients +function sendEmailTo(recepient) { + // But really it only logs out a string + console.log("email sent to " + recepient); +} + +const allEmails = + "benjamin@gmail.com|peter@gmail.com|hans@gmail.com|ahmad@gmail.com|sana@gmail.com|virgeen@gmail.com|mohammed@gmail.com"; +const allEmailsArray = allEmails.split("|"); + +for (let i = 0; i < allEmailsArray.length; i++) { + const email = allEmailsArray[i]; + sendEmailTo(email); +} + +// for(const email of allEmailsArray) { +// sendEmailTo(email); +// } + +// allEmailsArray.reverse().forEach(sendEmailTo) diff --git a/javascript1/week2/inclass/rj_inclass1.js b/javascript1/week2/inclass/rj_inclass1.js new file mode 100644 index 00000000..06bc4113 --- /dev/null +++ b/javascript1/week2/inclass/rj_inclass1.js @@ -0,0 +1,27 @@ +// Global scope +const globalScopeVariable = "globalScopeVariable"; +function outerFunction() { + // function scope A + console.log(globalScopeVariable); + const outerFunctionScope = "outerFunctionScope"; + + function innerFunction() { + // Function scope B + console.log(globalScopeVariable); + console.log(outerFunctionScope); + + const innerFunctionScope = "innerFunctionScope"; + } + + // console.log(innerFunctionScope); + + innerFunction(); +} + +outerFunction(); + +if (true) { + const blockScope = "blockScope"; +} + +console.log(blockScope); diff --git a/javascript1/week2/inclass/rj_inclass2.js b/javascript1/week2/inclass/rj_inclass2.js new file mode 100644 index 00000000..8c40c2b3 --- /dev/null +++ b/javascript1/week2/inclass/rj_inclass2.js @@ -0,0 +1,18 @@ +function spam(number) { + let repeated = ""; // Define the simplest case + + // Think about the condition. How many times do we want to repeat + for (let i = 0; i < number; i++) { + repeated = repeated + "hue"; // What is our update rule + } + + return repeated; // Return in the end +} + +console.log(spam(1)); + +// Test cases. I always do "empty" (here zero), once, and then a bunch of times +console.log(spam(0) === ""); +console.log(spam(1) === "hue"); +console.log(spam(6) === "huehuehuehuehuehue"); +console.log(spam(14) === "huehuehuehuehuehuehuehuehuehuehuehuehuehue"); diff --git a/javascript1/week2/inclass/rj_inclass3.js b/javascript1/week2/inclass/rj_inclass3.js new file mode 100644 index 00000000..bffb85ec --- /dev/null +++ b/javascript1/week2/inclass/rj_inclass3.js @@ -0,0 +1,7 @@ +const fruits = ["banana", "apple", "orange", "lemon", "plum"]; +// const fruits = "thisIsNotAFruit" + +for (let i = 0; i < fruits.length; i++) { + console.log(i); + console.log(fruits[i]); +} From b4d6e7a3096decf88bfee244821b6bd94a8427b7 Mon Sep 17 00:00:00 2001 From: Rasmus Jones Date: Sun, 5 May 2024 15:57:31 +0200 Subject: [PATCH 3/4] inclass files team 28 js1 week3 --- .../week3/inclass/.vscode/extensions.json | 6 +++ .../week3/inclass/.vscode/settings.json | 4 ++ javascript1/week3/inclass/hw_example_1.js | 25 +++++++++ javascript1/week3/inclass/hw_example_1_v2.js | 29 +++++++++++ javascript1/week3/inclass/hw_example_1_v3.js | 43 +++++++++++++++ javascript1/week3/inclass/hw_example_2.js | 23 ++++++++ javascript1/week3/inclass/inclass1.js | 52 +++++++++++++++++++ javascript1/week3/inclass/inclass2.js | 37 +++++++++++++ 8 files changed, 219 insertions(+) create mode 100644 javascript1/week3/inclass/.vscode/extensions.json create mode 100644 javascript1/week3/inclass/.vscode/settings.json create mode 100644 javascript1/week3/inclass/hw_example_1.js create mode 100644 javascript1/week3/inclass/hw_example_1_v2.js create mode 100644 javascript1/week3/inclass/hw_example_1_v3.js create mode 100644 javascript1/week3/inclass/hw_example_2.js create mode 100644 javascript1/week3/inclass/inclass1.js create mode 100644 javascript1/week3/inclass/inclass2.js diff --git a/javascript1/week3/inclass/.vscode/extensions.json b/javascript1/week3/inclass/.vscode/extensions.json new file mode 100644 index 00000000..7648e45a --- /dev/null +++ b/javascript1/week3/inclass/.vscode/extensions.json @@ -0,0 +1,6 @@ +{ + "recommendations": [ + "esbenp.prettier-vscode", // Prettier extension + "ritwickdey.LiveServer" // Live Server extension + ] +} diff --git a/javascript1/week3/inclass/.vscode/settings.json b/javascript1/week3/inclass/.vscode/settings.json new file mode 100644 index 00000000..9bf4d12b --- /dev/null +++ b/javascript1/week3/inclass/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "editor.defaultFormatter": "esbenp.prettier-vscode", + "editor.formatOnSave": true +} diff --git a/javascript1/week3/inclass/hw_example_1.js b/javascript1/week3/inclass/hw_example_1.js new file mode 100644 index 00000000..b6e423e8 --- /dev/null +++ b/javascript1/week3/inclass/hw_example_1.js @@ -0,0 +1,25 @@ +function getClothesToWear(temperature) { + if (temperature <= -10) { + return "Definitely bundle up in layers like an onion - keep warm!"; + } else if (temperature <= -5) { + return "Grab a thick jacket, but don't forget about style. Winter is still a fashionable season!"; + } else if (temperature <= 0) { + return "Who says a jacket and scarf can't be stylish accessories?"; + } else if (temperature <= 5) { + return "A stylish down jacket - your best friend during this time."; + } else if (temperature <= 10) { + return "Time for light jackets and spring boots!"; + } else if (temperature <= 15) { + return "A spring jacket and a pair of stylish sunglasses - the perfect combination"; + } else if (temperature <= 20) { + return "Of course, it's time for sneakers, shorts, and T-shirts!"; + } else if (temperature <= 25) { + return "Definitely just wear what you have on right now, because it's the perfect temperature!"; + } else if (temperature <= 30) { + return "T-shirt and shorts - always a fitting choice!"; + } else if (temperature > 40) { + return "Maybe it's time to get out the swimsuit or shorts and a shirt?"; +} + +const clothesToWear = getClothesToWear(23); +console.log(clothesToWear); diff --git a/javascript1/week3/inclass/hw_example_1_v2.js b/javascript1/week3/inclass/hw_example_1_v2.js new file mode 100644 index 00000000..b8dc812d --- /dev/null +++ b/javascript1/week3/inclass/hw_example_1_v2.js @@ -0,0 +1,29 @@ +// DATA +const clothes = [ + "Definitely bundle up in layers like an onion - keep warm!", + "Grab a thick jacket, but don't forget about style. Winter is still a fashionable season!", + "Who says a jacket and scarf can't be stylish accessories?", + "A stylish down jacket - your best friend during this time.", + "Time for light jackets and spring boots!", + "A spring jacket and a pair of stylish sunglasses - the perfect combination", + "Of course, it's time for sneakers, shorts, and T-shirts!", + "Definitely just wear what you have on right now, because it's the perfect temperature!", + "T-shirt and shorts - always a fitting choice!", + "Maybe it's time to get out the swimsuit or shorts and a shirt?", +]; +const temperatures = [-10, -5, 0, 5, 10, 15, 20, 25, 30]; + +function getClothesToWear(temperature, clothes, temperatures) { + // LOGIC + for (let i = 0; i < temperatures.length; i++) { + if (temperature < temperatures[i]) { + return clothes[i]; + } + } + return clothes[clothes.length - 1]; +} + +const clothesToWear = getClothesToWear(30, clothes, temperatures); + +// RENDER +console.log(clothesToWear); diff --git a/javascript1/week3/inclass/hw_example_1_v3.js b/javascript1/week3/inclass/hw_example_1_v3.js new file mode 100644 index 00000000..7674004b --- /dev/null +++ b/javascript1/week3/inclass/hw_example_1_v3.js @@ -0,0 +1,43 @@ +// DATA +const clothes = [ + "Definitely bundle up in layers like an onion - keep warm!", + "Grab a thick jacket, but don't forget about style. Winter is still a fashionable season!", + "Who says a jacket and scarf can't be stylish accessories?", + "A stylish down jacket - your best friend during this time.", + "Time for light jackets and spring boots!", + "A spring jacket and a pair of stylish sunglasses - the perfect combination", + "Of course, it's time for sneakers, shorts, and T-shirts!", + "Definitely just wear what you have on right now, because it's the perfect temperature!", + "T-shirt and shorts - always a fitting choice!", + "Maybe it's time to get out the swimsuit or shorts and a shirt?", +]; +const temperatures = [-10, -5, 0, 5, 10, 15, 20, 25, 30]; + +function getClothesToWear(temperature, clothes, temperatures) { + // LOGIC + for (let i = 0; i < temperatures.length; i++) { + if (temperature < temperatures[i]) { + return clothes[i]; + } + } + return clothes[clothes.length - 1]; +} +// LOGIC +const clothesToWear = getClothesToWear(30, clothes, temperatures); +// RENDER +console.log(clothesToWear); + +// now getClothesToWear is abstract enough to call it on different data +// DATA +const memos = [ + "Zero balance or less", + "Just enough to buy an icecream", + "Pizza time", + "Hair dresser", + "You are rich", +]; +const balances = [0, 10, 20, 30]; +// LOGIC +const statusOnYourCapital = getClothesToWear(15, memos, balances); +// RENDER +console.log(statusOnYourCapital); diff --git a/javascript1/week3/inclass/hw_example_2.js b/javascript1/week3/inclass/hw_example_2.js new file mode 100644 index 00000000..3ef119e5 --- /dev/null +++ b/javascript1/week3/inclass/hw_example_2.js @@ -0,0 +1,23 @@ +function getFullname(firstname, surname, useFormalName, isMale) { + // DATA + const malePrefix = "Mr "; + const femalePrefix = "Mrs "; + + // Logic + let prefix = ""; + if (useFormalName) { + if (isMale) { + prefix = malePrefix; + } else { + prefix = femalePrefix; + } + } + + // half rendering half logic + return `${prefix}${firstname} ${surname}`; +} + +outputName = getFullname("Benjamin", "Hughes", true, true); + +// Rendering +console.log(outputName); diff --git a/javascript1/week3/inclass/inclass1.js b/javascript1/week3/inclass/inclass1.js new file mode 100644 index 00000000..244e64c8 --- /dev/null +++ b/javascript1/week3/inclass/inclass1.js @@ -0,0 +1,52 @@ +const user = { + name: "Emil", + age: 25, + role: "Teacher", + classes: ["Javascript 1", "Javascript 2"], + hobbies: { + favourite: "computers", + sports: "running to class", + }, +}; +const user2 = { + name: "Peter", + age: 42, + role: "Teacher", + classes: ["Maths", "Physics"], + hobbies: { + favourite: "Raspberry Pi", + sports: "Tennis", + }, +}; + +const users = [user, user2]; +const keysToLog = ["name", "age", "hobbies"]; + +function renderObject(value) { + // ... + return "Not implemented yet"; +} + +function createOutput(users, keysToLog) { + const endOfInnerLoop = "\n"; + let output = ""; + for (let i = 0; i < users.length; i++) { + const user = users[i]; + for (let j = 0; j < keysToLog.length; j++) { + const key = keysToLog[j]; + const value = user[key]; + if (typeof value === "number" || typeof value === "string") { + output += value + " "; + } else if (typeof value === "object") { + output += renderObject(value); + } + } + output += endOfInnerLoop; + } + return output; +} + +console.log(createOutput(users, keysToLog)); + +// console.log(user2.name); +// console.log(user2["name"]); diff --git a/javascript1/week3/inclass/inclass2.js b/javascript1/week3/inclass/inclass2.js new file mode 100644 index 00000000..2b315a1a --- /dev/null +++ b/javascript1/week3/inclass/inclass2.js @@ -0,0 +1,37 @@ +const fruits = ["apple", "banana", "orange"]; +// Investigate array methods: pop, push, shift, unshift, includes + +// without touching the above lines, add "kiwi" to the fruits array +const count = fruits.push("kiwi"); +console.log(fruits); +console.log(count); + +// extract "kiwi" from the array and log it out +// the fruits array should now be without "kiwi" +const dropout = fruits.pop(); +console.log(fruits); +console.log(dropout); + +// without touching the above lines +// add dragonfruit and elderberry to the array +fruits.push("dragonfruit", "elderberry"); +console.log(fruits); + +// console.log "Banana in fruit basket" or "No Banana in ..." +// based on whether "banana" is inside of the fruit array +if (fruits.includes("banana")) { + console.log("Yes"); +} else { + console.log("No"); +} + +// add strawberry to the beginning of the fruit array +fruits.unshift("strawberry"); +console.log(fruits); + +// extract strawberry from the fruit array into a variable +// console log the fruit basket and the variable +const leftDropout = fruits.shift(); + +console.log(fruits); +console.log(leftDropout); From e7cd658b3ec86973260f9abe34b3d3aac144dc3a Mon Sep 17 00:00:00 2001 From: Rasmus Jones Date: Sun, 12 May 2024 16:08:37 +0200 Subject: [PATCH 4/4] team28 js1 week4 --- javascript1/week4/inclass/callstack.js | 24 ++++++++++++++ javascript1/week4/inclass/fibo.js | 37 +++++++++++++++++++++ javascript1/week4/inclass/fizzbuzz.js | 29 +++++++++++++++++ javascript1/week4/inclass/hw_discussion.js | 29 +++++++++++++++++ javascript1/week4/inclass/sentiment.js | 38 ++++++++++++++++++++++ 5 files changed, 157 insertions(+) create mode 100644 javascript1/week4/inclass/callstack.js create mode 100644 javascript1/week4/inclass/fibo.js create mode 100644 javascript1/week4/inclass/fizzbuzz.js create mode 100644 javascript1/week4/inclass/hw_discussion.js create mode 100644 javascript1/week4/inclass/sentiment.js diff --git a/javascript1/week4/inclass/callstack.js b/javascript1/week4/inclass/callstack.js new file mode 100644 index 00000000..3ec9fb41 --- /dev/null +++ b/javascript1/week4/inclass/callstack.js @@ -0,0 +1,24 @@ +function pickUpKids() { + // pickUpKids function gets pushed to call stack WHEN CALLED! + console.log("Kids picked up"); + // pickUpKids function now gets shifted from the call stack! +} + +function buyCarrots() {} + +function buyVegetables() { + buyCarrots(); +} + +function getGroceries() { + // buyVegetables pushed to call stack + buyVegetables(); + // buyVegetables shifted from call stack +} + +function doWork() {} + +pickUpKids(); +// getGroceries pushed to call stack +getGroceries(); +doWork(); diff --git a/javascript1/week4/inclass/fibo.js b/javascript1/week4/inclass/fibo.js new file mode 100644 index 00000000..23b9ce13 --- /dev/null +++ b/javascript1/week4/inclass/fibo.js @@ -0,0 +1,37 @@ +// 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 + +function fibArray(n) { + const fiboNumbers = [0, 1]; + for (let i = 2; i < n; i++) { + fiboNumbers.push(fiboNumbers[i - 1] + fiboNumbers[i - 2]); + } + + return fiboNumbers[n - 1]; +} + +function fibRec(n) { + if (n === 1) { + return 0; + } + if (n === 2) { + return 1; + } + return fibRec(n - 1) + fibRec(n - 2); +} + +function fibCorrect(n) { + const fiboNumbers = [0, 1]; + let newNumber; + for (let i = 2; i < n; i++) { + newNumber = fiboNumbers[0] + fiboNumbers[1]; + fiboNumbers[0] = fiboNumbers[1]; + fiboNumbers[1] = newNumber; + } + + return newNumber; +} + +// console.log(fibCorrect(5)); +// console.log(fibCorrect(10)); + +console.log(fibCorrect(1000)); diff --git a/javascript1/week4/inclass/fizzbuzz.js b/javascript1/week4/inclass/fizzbuzz.js new file mode 100644 index 00000000..0dba23ba --- /dev/null +++ b/javascript1/week4/inclass/fizzbuzz.js @@ -0,0 +1,29 @@ +function fizzBuzz(a, b) { + const fizzBuzzArray = []; + for (let i = 1; i < 100; i++) { + // console.log(i); + if (i % a === 0 && i % b === 0) { + // multiple of a and b print fizzBuzz + // console.log("fizzBuzz"); + fizzBuzzArray.push("fizzBuzz"); + } else if (i % a === 0) { + // multiple of a print fizz + // console.log("fizz"); + fizzBuzzArray.push("fizz"); + } else if (i % b === 0) { + // multiple of b print buzz + // console.log("buzz"); + fizzBuzzArray.push("buzz"); + } else { + // console.log(i); + fizzBuzzArray.push(i); + } + } + return fizzBuzzArray; +} + +const output = fizzBuzz(3, 5); +console.log(output); + +const output2 = fizzBuzz(4, 10); +console.log(output2); diff --git a/javascript1/week4/inclass/hw_discussion.js b/javascript1/week4/inclass/hw_discussion.js new file mode 100644 index 00000000..878b02da --- /dev/null +++ b/javascript1/week4/inclass/hw_discussion.js @@ -0,0 +1,29 @@ +const seriesDurations = [ + { + title: "Game of thrones", + days: 3, + hours: 1, + minutes: 0, + }, + { + title: "Sopranos", + days: 3, + hours: 14, + minutes: 0, + }, + { + title: "The Wire", + days: 2, + hours: 12, + minutes: 0, + }, +]; + +function logOutSeriesText({ title, days }) { + // const { title, days } = singleSeries; + // Some other code that does stuff + + console.log(title, days); +} + +logOutSeriesText(seriesDurations[0]); diff --git a/javascript1/week4/inclass/sentiment.js b/javascript1/week4/inclass/sentiment.js new file mode 100644 index 00000000..1b25bd89 --- /dev/null +++ b/javascript1/week4/inclass/sentiment.js @@ -0,0 +1,38 @@ +function createArrayOfOverlappingWords(sentenceArray, wordsList) { + const words = []; + for (let i = 0; i < sentenceArray.length; i++) { + if (wordsList.includes(sentenceArray[i])) { + words.push(sentenceArray[i]); + } + } + return words; +} +function getSentimentScore(sentence, positiveWordsList, negativeWordsList) { + const sentenceArray = sentence.split(" "); // ["I", "am", "mega", "super", ...] + + const positiveWords = createArrayOfOverlappingWords( + sentenceArray, + positiveWordsList + ); + const negativeWords = createArrayOfOverlappingWords( + sentenceArray, + negativeWordsList + ); + + return { + score: positiveWords.length - negativeWords.length, + negativeWords, + positiveWords, + }; +} + +const positiveWordsList = ["happy", "awesome", "mega", "super"]; +const negativeWordsList = ["sad", "boring", "hate", "awful"]; + +const sentimentScoreObject = getSentimentScore( + "I am mega super awesome happy awful", + positiveWordsList, + negativeWordsList +); + +console.log(sentimentScoreObject);