diff --git a/Week1/homework/calculator/bkgd.png b/Week1/homework/calculator/bkgd.png new file mode 100644 index 000000000..d457eb3e6 Binary files /dev/null and b/Week1/homework/calculator/bkgd.png differ diff --git a/Week1/homework/calculator/calc.svg b/Week1/homework/calculator/calc.svg new file mode 100644 index 000000000..bb63137db --- /dev/null +++ b/Week1/homework/calculator/calc.svg @@ -0,0 +1,324 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Week1/homework/calculator/index.html b/Week1/homework/calculator/index.html new file mode 100644 index 000000000..fad118b57 --- /dev/null +++ b/Week1/homework/calculator/index.html @@ -0,0 +1,53 @@ + + + + + + + Simple Calculator + + + + + + +
+

Simple Calculator

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + \ No newline at end of file diff --git a/Week1/homework/calculator/main.css b/Week1/homework/calculator/main.css new file mode 100644 index 000000000..ac6447b1c --- /dev/null +++ b/Week1/homework/calculator/main.css @@ -0,0 +1,88 @@ +body{ + background-color: burlywood; +} +.container { + background-image: url(bkgd.png); + background-position: inherit; + background-repeat: no-repeat; + text-align: center; + width: 309px; + height: 500px; + padding:inherit; +} + +h4 { + color: lightblue; + text-align: left; + padding-left: 35px; + padding-top: 15px; +} + +button { + width: 60px; + height: 50px; + font-size: 22px; + background-color: gray; + color: #fee; + border: 4px gray outset; + border-radius: 9px; + cursor: pointer; +} + +.top { + height: 30px; + background-color: li; + box-shadow: 3px 2px 3px black; + border-radius: 4px; + border: 1px outset; + color: #bec; + text-shadow: 2px 2px 20px white; + font-size: 20px; +} + +#c { + width: 80%; + height: 50px; + color: black; +} + +button:active { + background-color: gray; + outline: 1px solid grey; + border: 3px gainsboro inset; +} + +.top:hover{ + box-shadow: inset 5px 5px 5px grey, inset -5px -5px 5px white; +} + +button:hover{ + background-color: gainsboro; +} + +.top:active{ + background-color: gainsboro; + outline: 1px solid gray; + border: 3px gainsboro inset; +} +#display-container { + border: 19px inset; + margin-bottom: 29px; +} + +#h1, #display1 { + width: inherit; + height: 25px; + background-color:powderblue; + font-size: 20px; + font-family: serif; + font-weight: bold; + text-align: right; + overflow: hidden; + color: black; + text-shadow: 2px 2px 1px #9aa; +} +#display1 { + font-size: 16px; + padding-bottom: 10px; +} \ No newline at end of file diff --git a/Week1/homework/calculator/main.js b/Week1/homework/calculator/main.js new file mode 100644 index 000000000..bc39ef2b3 --- /dev/null +++ b/Week1/homework/calculator/main.js @@ -0,0 +1,208 @@ +let decimalPoint, enter, entered, operatorSign, rootNpower_Sign, flo, firstI, first, second, secondI, answer, theanswer, result; +let opsCheck, dotCounter; +let removeFirstZero; + +function dot(b) { + if (decimalPoint == "") { + enter = entered = b; + first+= enter; + entered+= enter; + decimalPoint = "."; + dotCounter = 0; + return first; + } else { + return first; + } +} + +function c(c) { + document.getElementById("display1").innerHTML = decimalPoint = operatorSign = rootNpower_Sign = entered = math = first = firstI = second = secondI = answer = theanswer = flo = M = ""; + return ""; +} + +c(); + +function maths(a) { + math = a; + try { + if (operatorSign == "+") { + firstI = first - second; + mathematics(); + first = second + "+" + result; + } else if (operatorSign == "-") { + firstI = second - first; + mathematics(); + first = second + "-" + "(" + result + ")"; + } else if (operatorSign == "*") { + firstI = first / second; + mathematics(); + first = second + "*" + result; + } else if (operatorSign == "/") { + firstI = second / first; + mathematics(); + first = second + "/" + result; + } else { + firstI = first; + mathematics(); + first = result; + } + return first; + } catch (first ) { + first = second + operatorSign; + return first; + } +} + +function digit(b) { + opsCheck = 0; + dotCounter++; + if (first == Infinity || first == NaN) { + first = 0; + } + peSign = "pes"; + entered = b; + if (rootNpower_Sign != "") { + first = (first === "0" && entered !== ".") ? entered: first + entered; + return secondI + firstI + rootNpower_Sign + first; + } else { + first = (first === "0" && entered !== ".") ? entered: first + entered; + theanswer = eval(first) + ""; + if (theanswer.length > 14) { + theanswer = Math.abs((theanswer*1).toPrecision(14)); + } + document.getElementById("display1").innerHTML = first; + return theanswer; + } +} + +function operators(b) { + peSign = ""; + if (opsCheck == 0) { + opsCheck = 1; + document.getElementById("display1").innerHTML = first; + try { + if (rootNpower_Sign == "^") { + if (operatorSign == "+") { + answer = result + second; + } else if (operatorSign == "-") { + answer = second - result; + } else if (operatorSign == "*") { + answer = result * second; + } else if (operatorSign == "/") { + answer = second / result; + } else { + answer = result; + } + } else if (rootNpower_Sign == "√") { + if (operatorSign == "+") { + answer = second + result; + } else if (operatorSign == "-") { + answer = second - result; + } else if (operatorSign == "*") { + answer = result * second; + } else if (operatorSign == "/") { + answer = second / result; + } else { + answer = result; + } + } else if (a == "%") { + answer = second % first; + } else { + operatorSign = b; + first += operatorSign; + decimalPoint = ""; + } + rootNpower_Sign = ""; + operatorSign = b; + firstI = ""; + second = answer; + first = answer + operatorSign; + decimalPoint = ""; + +document.getElementById("display1").innerHTML = first; + return eval(second); + } catch(x) { + if (first != "Press ON to start") { + operatorSign = b; + second = eval(first); + first += operatorSign; + decimalPoint = ""; + } else { + first = "Press ON to start" ; + } + document.getElementById("display1").innerHTML = first; + return (second == undefined ) ? 0 : eval(second); + } + } else { + operatorSign = b; + first += ""; + first = first.substr(0, first.length - 1); + first = first + operatorSign; + document.getElementById("display1").innerHTML = first; + return (second == undefined ) ? 0 : eval(second); + } +} + +function equal() { + document.getElementById("display1").innerHTML = first; + try { + if (rootNpower_Sign == "^") { + if (operatorSign == "+") { + first = result + second; + } else if (operatorSign == "-") { + first = second - result; + } else if (operatorSign == "*") { + answer = result * second; + first = answer; + } else if (operatorSign == "/") { + first = second / result; + } else { + first = result; + } + } else if (rootNpower_Sign == "√") { + if (operatorSign == "+") { + first = result + second; + } else if (operatorSign == "-") { + first = second - result; + } else if (operatorSign == "*") { + first = result * second; + } else if (operatorSign == "/") { + first = second / result; + } else { + first = result; + } + } else if (operatorSign == "%") { + answer = second % first; + } else { + if (first == "") { + first = first ; + } else { + try{ + first = eval(first) + ""; + if (first.length > 14) { + first = Math.abs((first*1).toPrecision(14)); + } + } catch (first) { + first = "Incorrect input. Click C to clear"; + return first; + first = ""; + } + } + } + rootNpower_Sign = operatorSign = answer = firstI = second = ""; + flo = first; + flo = Math.floor(flo); + decimalPoint = (flo == first) ? "": "."; + return first; + } catch(operatorSign) { + operatorSign = ""; + first = eval(first) + ""; + if (first.length > 14) { + first = Math.abs((first*1).toPrecision(14)); + } + flo = first; + flo = Math.floor(flo); + decimalPoint = (flo == first) ? "": "."; + return first; + } +} \ No newline at end of file diff --git a/Week1/homework/index.html b/Week1/homework/index.html deleted file mode 100644 index e69de29bb..000000000 diff --git a/Week1/homework/js-exercises/JS1_w1e10_arrCompare.js b/Week1/homework/js-exercises/JS1_w1e10_arrCompare.js new file mode 100644 index 000000000..33732efb2 --- /dev/null +++ b/Week1/homework/js-exercises/JS1_w1e10_arrCompare.js @@ -0,0 +1,15 @@ +'use strict' + +const arr1 = [123,234,0,1]; //1 +const arr2 = [192,168,1,1,255,0,0]; //1 + +console.log("The length of the arr1 is " + arr1.length + " and the length of the arr2 is " + arr2.length); //2 + +if(arr1.length === arr2.length) console.log("arr1 and arr2 has the same length"); //3 +else console.log("arr1 and arr2 has not the same length"); //3 + +/* +1. Declare 2 variables, that each hold an array. The first array should have 4 items, the second 7 items +2. Find out how to get the length of each array. Write a `console.log` statement that shows the length of each array +3. Write a conditional statement that checks if both are of equal length. If they are, log to the console `They are the same!`, if not log `Two different sizes` +*/ \ No newline at end of file diff --git a/Week1/homework/js-exercises/JS1_w1e1_helloWorld.js b/Week1/homework/js-exercises/JS1_w1e1_helloWorld.js new file mode 100644 index 000000000..44cf5c04e --- /dev/null +++ b/Week1/homework/js-exercises/JS1_w1e1_helloWorld.js @@ -0,0 +1,12 @@ +'use strict' + +console.log("Hello World"); // English +console.log("Γειά σου Κόσμε"); // Greek +console.log("Labas Pasauli"); // Lithuanian +console.log("Hallo Welt"); // German +console.log("Selam Dünya"); // Turkish +console.log("Ciao Mondo"); // Italian +console.log("Witaj świecie"); // Polish +console.log("Hei Verden"); // Norwegian +console.log("Hej Världen"); // Swidish +console.log("Ahoj Světe"); // Czech \ No newline at end of file diff --git a/Week1/homework/js-exercises/JS1_w1e2_errorDebug.js b/Week1/homework/js-exercises/JS1_w1e2_errorDebug.js new file mode 100644 index 000000000..a31e1f450 --- /dev/null +++ b/Week1/homework/js-exercises/JS1_w1e2_errorDebug.js @@ -0,0 +1,3 @@ +'use strict' + +console.log("I'm awesome"); \ No newline at end of file diff --git a/Week1/homework/js-exercises/JS1_w1e3_logTheNum.js b/Week1/homework/js-exercises/JS1_w1e3_logTheNum.js new file mode 100644 index 000000000..0ab5d84cc --- /dev/null +++ b/Week1/homework/js-exercises/JS1_w1e3_logTheNum.js @@ -0,0 +1,19 @@ +'use strict' + + +let numberX; //1 +console.log("numberX is undefined."); //2 +console.log(numberX); //3 +numberX = 1; //4 +console.log("now numberX is integer."); //5 +console.log(numberX); //6 + + +/*Follow the steps. Make sure that each step is written on the line after. + +1. First, declare your variable `numberX`. Do not _initialize_ it (which means, don't give it a value) yet. +2. Add a `console.log` statement that explains in words _what you think_ the value of `x` is, like in this example. +3. Add a `console.log` statement that logs the value of `numberX`. +4. Now _initialize_ your variable `numberX` with a number (also called an `integer` in computer science terms). +5. Next, add a `console.log` statement that explains _what you think_ the value of `numberX` is. +6. Add a `console.log` statement that logs the value of `numberX`.*/ \ No newline at end of file diff --git a/Week1/homework/js-exercises/JS1_w1e4_logTheString.js b/Week1/homework/js-exercises/JS1_w1e4_logTheString.js new file mode 100644 index 000000000..7f36d2cdb --- /dev/null +++ b/Week1/homework/js-exercises/JS1_w1e4_logTheString.js @@ -0,0 +1,16 @@ +'use strict' + +let myString = "George Mouzakis"; //1 +console.log("myString variable is a string.") //2 +console.log(myString); //3 +myString = "Hello World"; //4 +console.log("myString variable still to be a string.") //5 +console.log(myString); //6 + +/* +1. Declare a variable `myString` and assign a string to it. Use your full name, including spaces, as the content for the string. +2. Write a `console.log` statement in which you explain in words _what you think_ the value of the string is. +3. Now `console.log` the variable `myString`. +4. Now reassign to the variable `myString` a new string. +5. Just like what you did before write a `console.log` statement that explains in words _what you think_ will be logged to the console. +6. Now console.log `myString` again.*/ \ No newline at end of file diff --git a/Week1/homework/js-exercises/JS1_w1e5_roundAndLog.js b/Week1/homework/js-exercises/JS1_w1e5_roundAndLog.js new file mode 100644 index 000000000..de6bdc15c --- /dev/null +++ b/Week1/homework/js-exercises/JS1_w1e5_roundAndLog.js @@ -0,0 +1,18 @@ +'use strict' + +let z = 7.25; //1 +console.log(z); //2 +let a = Math.round(z); //3 +console.log(a); //4 +let x = 0; +if (z=0 && score<=49) grade = 'F'; //2* We check which score matches with which grade + else if(score>=50 && score<=59) grade = 'E'; + else if(score>=60 && score<=69) grade = 'D'; + else if(score>=70 && score<=79) grade = 'C'; + else if(score>=80 && score<=89) grade = 'B'; + else if(score>=90 && score<=100) grade = 'A'; //*2 + + if(score>=0 && score<=100) console.log("You got a "+grade+" ("+score+"%)!"); //1 , 3 We print the results to console and score to percentage convertion + else console.log("Wrong input, your score must be between 0 and 100"); +} + +gradeCalculator(Math.floor(Math.random()*100)); + +/* +1. convert the score into a percentage +2. calculate what grade corresponds with that percentage, and +3. shows in the command line the result: the grade and the percentage +*/ \ No newline at end of file diff --git a/Week2/homework/homework.md b/Week2/homework/homework.md new file mode 100644 index 000000000..d5b83cff9 --- /dev/null +++ b/Week2/homework/homework.md @@ -0,0 +1 @@ +homework folder \ No newline at end of file diff --git a/Week2/homework/js-exercises/JS1_w2e1_removeComma.js b/Week2/homework/js-exercises/JS1_w2e1_removeComma.js new file mode 100644 index 000000000..5c2124133 --- /dev/null +++ b/Week2/homework/js-exercises/JS1_w2e1_removeComma.js @@ -0,0 +1,13 @@ +'use strict'; + +let myString = "hello,this,is,a,difficult,to,read,sentence"; //1 +console.log(myString.length); //2 +for(let i = 0; i<=myString.length; i++) myString = myString.replace(',',' '); //3 +console.log(myString); //4 + +/* +1. Add the variable to your file. +2. Log the length of `myString`. +3. The commas make that the sentence is quite hard to read. Find a way to remove the commas from the string and replace them with spaces. (use Google!) +4. After replacing the commas, log `myString` to see if you succeeded. +*/ \ No newline at end of file diff --git a/Week2/homework/js-exercises/JS1_w2e2_evenOddReporter.js b/Week2/homework/js-exercises/JS1_w2e2_evenOddReporter.js new file mode 100644 index 000000000..8f9d8cb7c --- /dev/null +++ b/Week2/homework/js-exercises/JS1_w2e2_evenOddReporter.js @@ -0,0 +1,14 @@ +'use strict'; + +for(let i = 0; i<=20; i++){ //1 + if(i%2==0) //2 + console.log(i + " is even"); //3 + else console.log(i + " is odd"); //4 +} + +/* +1. Create a `for` loop, that iterates from 0 to 20. +2. Create a conditional statement that checks if the value of the counter variable is odd or even. +3. If it's odd, log to the console `The number [PUT_NUMBER_HERE] is odd!`. +4. If it's even, log to the console `The number [PUT_NUMBER_HERE] is even!`. +*/ diff --git a/Week2/homework/js-exercises/JS1_w2e3_theRecipeCard.js b/Week2/homework/js-exercises/JS1_w2e3_theRecipeCard.js new file mode 100644 index 000000000..12fdcba92 --- /dev/null +++ b/Week2/homework/js-exercises/JS1_w2e3_theRecipeCard.js @@ -0,0 +1,17 @@ +'use strict'; + + let mealRecipe = { //1 + title : "Toast", //2* + servings : 1, + ingredients : ["\ncheese: 2" , "\ntomato: 2" , "\nham: 2" , "\negg: 1"] //*2 +}; + +for (const recipe in mealRecipe) { //3* + console.log(`${recipe}:${mealRecipe[recipe]}`); +}; //*3 + +/* +1. Declare a variable that holds an object (your meal recipe). +2. Give the object 3 properties: a `title` (string), a `servings` (number) and an `ingredients` (array of strings) property. +3. Log each property out seperately, using a loop (for, while or do/while) +*/ \ No newline at end of file diff --git a/Week2/homework/js-exercises/JS1_w2e4_theReadingList.js b/Week2/homework/js-exercises/JS1_w2e4_theReadingList.js new file mode 100644 index 000000000..d7c0fbb1c --- /dev/null +++ b/Week2/homework/js-exercises/JS1_w2e4_theReadingList.js @@ -0,0 +1,21 @@ +'use strict'; + +let books = [{title: "The Hobbit", author:"J.R.R. Tolkien", alreadyRead: false}, //1* + {title: "The Lord of the Rings", author:"J.R.R. Tolkien", alreadyRead: false}, + {title: "The Fellowship of the Ring", author:"J.R.R. Tolkien", alreadyRead: true} + ]; //*1 + + +for(let i=0; iNumber of Characters: ${len} `); + }; + + function lengthValidator(paramIN){ // this function checks if the card has the right length, 16 digits + if (paramIN.length == 16) { + cardLength.classList.remove('invalid'); + cardLength.classList.add('valid'); + } + else { + cardLength.classList.remove('valid'); + cardLength.classList.add('invalid'); + } + }; + + function numValidator(paramIN){ + const upperCase = /[A-Z]/g; //using regex we check if the input character is uppercase + const lowerCase = /[a-z]/g; // or lowercase + const symbols = /[$-/:-?{-~!"^_`\[\]@#]/g; // or symbols + if (paramIN.match(upperCase) || paramIN.match(lowerCase) || paramIN.match(symbols)){ + onlyNumbers.classList.remove('valid'); + onlyNumbers.classList.add('invalid'); + } + else { + onlyNumbers.classList.remove('invalid'); + onlyNumbers.classList.add('valid'); + } + }; + + function notSameDigs(paramIN){ + const numbers = /([0-9])\1{15}/g; // numbers takes value when a number has the same digit for 16 times + if (paramIN.match(numbers)){ // if that happen, then the if evaluates true + notSameDigits.classList.remove('valid'); + notSameDigits.classList.add('invalid'); + } + else { + notSameDigits.classList.remove('invalid'); + notSameDigits.classList.add('valid'); + } + }; + + function lastNumEven(paramIN){ + const num = /^\d*[02468]$/g; + if (paramIN.match(num)){ + lastEven.classList.remove('invalid'); + lastEven.classList.add('valid'); + } + else{ + lastEven.classList.remove('valid'); + lastEven.classList.add('invalid'); + } + }; + + function numbersTotal(paramIN){ + let value = paramIN; + let sum = value.toString().split('').map(Number).reduce(function (a, b){ + return a + b; + }, 0); + + if (sum>=16){ + numTotal.classList.remove('invalid'); + numTotal.classList.add('valid'); + } + else{ + numTotal.classList.remove('valid'); + numTotal.classList.add('invalid'); + } + }; + + + userIN.addEventListener('keyup', (event) => { + const cardNumber = userIN.value; + lengthCounter(cardNumber); + lengthValidator(cardNumber); + numValidator(cardNumber); + notSameDigs(cardNumber); + lastNumEven(cardNumber); + numbersTotal(cardNumber); + }, false); + +}; + +/* +- Number must be 16 digits, all of them must be numbers +- You must have at least two different digits represented (all of the digits cannot be the same) +- The final digit must be even +- The sum of all the digits must be greater than 16 +- The following credit card numbers are valid: + +```markdown +a92332119c011112 (invalid characters) +4444444444444444 (only one type of number) +1111111111111110 (sum less than 16) +6666666666666661 (odd final number) +``` +*/ \ No newline at end of file diff --git a/Week3/homework/CreditCard Validator/index.html b/Week3/homework/CreditCard Validator/index.html new file mode 100644 index 000000000..12670844a --- /dev/null +++ b/Week3/homework/CreditCard Validator/index.html @@ -0,0 +1,28 @@ + + + + + CreditCard Validator GMouzakis + + + + + +
+ + +
+
+

Your card is:

+

Number of Characters:

+

1a. Number must be 16 digits

+

1b. All of them must be numbers

+

2. You must have at least two different digits represented + (all of the digits cannot be the same) +

+

3. The final digit must be even

+

4. The sum of all the digits must be greater than 16

+ +
+ + \ No newline at end of file diff --git a/Week3/homework/CreditCard Validator/style.css b/Week3/homework/CreditCard Validator/style.css new file mode 100644 index 000000000..4fe5a061d --- /dev/null +++ b/Week3/homework/CreditCard Validator/style.css @@ -0,0 +1,50 @@ +input { + width: 50%; + padding: 10px; + border: 1px solid #CCC; + border-radius: 5px; + box-sizing: border-box; +} + +.container { + background-color: #EEE; + padding: 20px; +} + +#message { + + background: #EEE; + color: #000; + position: relative; + padding: 20px; + margin-top: 10px; +} + +#message p { + font-size: 15px; +} + +h3 { + text-align:center; +} +/* Add a green text color and a checkmark when the requirements are right */ +.valid { + color: green; +} + +.valid:before { + position: relative; + left: -5px; + content: "✓"; +} + +/* Add a red text color and an Xmark when the requirements are wrong */ +.invalid { + color: red; +} + +.invalid:after { + position: relative; + left: 5px; + content: "✗"; +} \ No newline at end of file diff --git a/Week3/homework/README.md b/Week3/homework/README.md new file mode 100644 index 000000000..eb8b01bb1 --- /dev/null +++ b/Week3/homework/README.md @@ -0,0 +1 @@ +JS1_w3 exercises diff --git a/Week3/homework/js-exercises/JS1_w3e1_giveCompliment.js b/Week3/homework/js-exercises/JS1_w3e1_giveCompliment.js new file mode 100644 index 000000000..d002c3063 --- /dev/null +++ b/Week3/homework/js-exercises/JS1_w3e1_giveCompliment.js @@ -0,0 +1,23 @@ +'use strict'; + +function giveCompliment(yourname){ //1 + + const compliments = ["great","awesome","hacker","fast","strong","smart","clever","programmer","hired","powerful"]; //1a + let rand = Math.floor(Math.random() * compliments.length); //1b + console.log(`You are ${compliments[rand]}, ${yourname}!`); //1c +} + +giveCompliment("George"); //2* +giveCompliment("George"); +giveCompliment("George"); //*2 + +/* +1. Write a function named `giveCompliment` + +- It takes 1 argument: your name +- Inside the function create an array with 10 strings. Each string should be a compliment, like `"great"`, `"awesome"` +- Write logic that randomly selects a compliment +- Return a string: "You are [COMPLIMENT], [YOUR_NAME]! + +2. Call the function three times, giving each function call the same argument: your name. +*/ \ No newline at end of file diff --git a/Week3/homework/js-exercises/JS1_w3e2_calcDogYears.js b/Week3/homework/js-exercises/JS1_w3e2_calcDogYears.js new file mode 100644 index 000000000..dc159d0ab --- /dev/null +++ b/Week3/homework/js-exercises/JS1_w3e2_calcDogYears.js @@ -0,0 +1,17 @@ +'use strict'; + +function calculateDogAge(age){ //1, 1a + age*=7; //1b + return (`Your doggie is ${age} years old in dog years!`) //1c +}; + +console.log(calculateDogAge(8)); + + + +/*1. Write a function named `calculateDogAge`. + +- It takes 1 argument: your puppy's age (number). +- Calculate your dog's age based on the conversion rate of 1 human year to 7 dog years. +- Return a string: "Your doggie is [CALCULATED_VALUE] years old in dog years!" +*/ \ No newline at end of file diff --git a/Week3/homework/js-exercises/JS1_w3e3_tellFortune.js b/Week3/homework/js-exercises/JS1_w3e3_tellFortune.js new file mode 100644 index 000000000..6d28760cb --- /dev/null +++ b/Week3/homework/js-exercises/JS1_w3e3_tellFortune.js @@ -0,0 +1,30 @@ +'use strict'; + +const numChildren = [2, 4, 6, 5, 12]; //2* +const partnerNames = ["Maria","Giota","Christina","Kostantina","Kaliopi"]; +const locations = ["Greece","Italy","Africa","America","Russia"]; +const jobs = ["programmer","chef","electrician","engineer","lawyer"]; //*2 + +function tellFortune(childrenCount,partnersName,location,jobTitle){ //1a + childrenCount = Math.floor(Math.random() * numChildren.length); //1b* + partnersName = Math.floor(Math.random() * partnerNames.length); + location = Math.floor(Math.random() * locations.length); + jobTitle = Math.floor(Math.random() * jobs.length); //*1b + + return (`You will be a ${jobs[jobTitle]} in ${locations[location]}, and married to ${partnerNames[partnersName]} with ${numChildren[childrenCount]} kids.`); //1c +} + +console.log(tellFortune()); //3 + + + +/* +1. Write a function named `tellFortune`. + +- It takes 4 arguments: number of children (number), partner's name (string), geographic location (string), job title (string). +- Randomly select values from the arrays. +- Return a string: "You will be a [JOB_TITLE] in [LOCATION], and married to [PARTNER_NAME] with [NUMBER_KIDS] kids." + +2. Create 4 arrays, `numChildren`, `partnerNames`, `locations` and `jobs`. Give each array 5 random values that make sense +3. Call the function 1 time, by passing the arrays as the argument. +*/ \ No newline at end of file diff --git a/Week3/homework/js-exercises/JS1_w3e4_addToShoppingCart.js b/Week3/homework/js-exercises/JS1_w3e4_addToShoppingCart.js new file mode 100644 index 000000000..76087f412 --- /dev/null +++ b/Week3/homework/js-exercises/JS1_w3e4_addToShoppingCart.js @@ -0,0 +1,29 @@ +'use strict'; + +const shoppingCart = [" bananas"," milk"]; //2 + +function addToShoppingCart(addItem){ //1 , 1a + shoppingCart.push(addItem); //1b* + if(shoppingCart.length == 4) shoppingCart.shift(); //*1b + return console.log("You bought" + shoppingCart.join() + "."); //1c +} + +addToShoppingCart(" eggs"); //3* +addToShoppingCart(" apples"); +addToShoppingCart(" fish"); //*3 + + +/* +Let's do some grocery shopping! We're going to get some things to cook dinner with. +However, you like to spend your money and always buy too many things. +So when you have more than 3 items in your shopping cart the first item gets taken out. + +1. Write a function named `addToShoppingCart`. + +- It takes in 1 argument: a grocery item (string) +- Add grocery item to array. If the amount of items is more than 3 remove the first one in the array +- Return a string: "You bought [LIST_OF_GROCERY_ITEMS]!" + +2. Create an array with 2 predefined strings: `"bananas"` and `"milk"` +3. Call the function 3 times, each time with a different string as the argument. +*/ \ No newline at end of file diff --git a/Week3/homework/js-exercises/JS1_w3e5_totalCost.js b/Week3/homework/js-exercises/JS1_w3e5_totalCost.js new file mode 100644 index 000000000..22039eab7 --- /dev/null +++ b/Week3/homework/js-exercises/JS1_w3e5_totalCost.js @@ -0,0 +1,28 @@ +'use strict'; + +const cartForParty = {beers:1.75, chips:0.99, cola:1.50, pizza:5.90, energydrink:1.20}; //2 + +function calculateTotalPrice(obj){ //1, 1a + const values = Object.values(obj); + let total = 0; + for (const value of values) total+=value; //1b + return total; //1c +} + +calculateTotalPrice(cartForParty); //3 + +console.log(`You have to pay $${calculateTotalPrice(cartForParty)}.`); + +/* +You want to buy a couple of things from the supermarket to prepare for a party. +After scanning all the items the cashier gives you the total price, but the machine a broken! Let's write her a function that does it for her instead! + +1. Write a function called `calculateTotalPrice` + +- It takes 1 argument: an object that contains properties that only contain number values +- Add all the number values together +- Return a number: the total price of all items + +2. Create an object named `cartForParty` with 5 properties. Each property should be a grocery item (like `beers` or `chips`) and hold a number value (like `1.75` or `0.99`) +3. Call the function 1 time, giving it the object `cartForParty` as an argument +*/ \ No newline at end of file