diff --git a/Week1/homework/js calculator/index.css b/Week1/homework/js calculator/index.css new file mode 100644 index 000000000..509f95b5e --- /dev/null +++ b/Week1/homework/js calculator/index.css @@ -0,0 +1,29 @@ +body{ + width: 1920px; + height: 1024px; +} + +.main, table{ +width: 400px; +height: 400px; +background-color: gainsboro; +} + + + +.textview{ +background-color: greenyellow; +width: 100%; +border:4px; +font-size: 40px; +border-style:inset; + text-align: center; +} + +.button{ + background-color: silver; + width: 100%; + height: 100%; + font-size: 30px; + border-radius: 15px; +} diff --git a/Week1/homework/js calculator/index.html b/Week1/homework/js calculator/index.html new file mode 100644 index 000000000..2dbb13980 --- /dev/null +++ b/Week1/homework/js calculator/index.html @@ -0,0 +1,44 @@ + + + + + +
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + \ No newline at end of file diff --git a/Week1/homework/js calculator/main.js b/Week1/homework/js calculator/main.js new file mode 100644 index 000000000..3e96fbbcf --- /dev/null +++ b/Week1/homework/js calculator/main.js @@ -0,0 +1,20 @@ +function insert(num){ +document.form.textview.value = document.form.textview.value+num +} + +function equal(){ +var exp = document.form.textview.value + +if(exp){ document.form.textview.value = eval(exp) + } + } +function clean(){ + document.form.textview.value = "" +} + +function back(){ + var exp = document.form.textview.value + + document.form.textview.value = exp.substring(0,exp.length-1) + } + diff --git a/Week1/homework/js-exercises/10_compareArrays.js b/Week1/homework/js-exercises/10_compareArrays.js new file mode 100644 index 000000000..5b21cfa59 --- /dev/null +++ b/Week1/homework/js-exercises/10_compareArrays.js @@ -0,0 +1,15 @@ +'use strict' + +//Declare 2 variables, that each hold an array. The first array should have 4 items, the second 7 items +const array1=["one",1,true,{name:"array1"}]; +const array2=["one",2,3,"four","five",6,7]; + +//Find out how to get the length of each array. Write a console.log statement that shows the length of each array +console.log("Array1 has length of "+ array1.length); +console.log("Array2 has length of "+ array2.length); + +//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 +if(array1==array2){ + console.log("They are the same!"); +} +else console.log("Two different sizes"); \ No newline at end of file diff --git a/Week1/homework/js-exercises/1_logHello.js b/Week1/homework/js-exercises/1_logHello.js new file mode 100644 index 000000000..09dba1901 --- /dev/null +++ b/Week1/homework/js-exercises/1_logHello.js @@ -0,0 +1,11 @@ +'use strict' +console.log("Hello, World!") +console.log("Geia sou, Kosme!") +console.log("Ciao, Mondo!") +console.log("Bonjour, le Monde!") +console.log("Hallo, Welt!") +console.log("Hola, Mundo!") +console.log("Hei, Verden!") +console.log("salve, Mundi!") +console.log("Selam, Dunya!") +console.log("Witaj, Wiecie!") \ No newline at end of file diff --git a/Week1/homework/js-exercises/2_ErrorDebugging.js b/Week1/homework/js-exercises/2_ErrorDebugging.js new file mode 100644 index 000000000..abfb656f2 --- /dev/null +++ b/Week1/homework/js-exercises/2_ErrorDebugging.js @@ -0,0 +1,2 @@ +'use strict' +console.log("I'm Awesome") \ No newline at end of file diff --git a/Week1/homework/js-exercises/3_LogNumber.js b/Week1/homework/js-exercises/3_LogNumber.js new file mode 100644 index 000000000..4d1813ab8 --- /dev/null +++ b/Week1/homework/js-exercises/3_LogNumber.js @@ -0,0 +1,18 @@ +'use strict' +/*First, declare your variable numberX. Do not initialize it (which means, don't give it a value) yet.*/ +var numberX + +/*Add a console.log statement that explains in words what you think the value of x is, like in this example.*/ +console.log("The value of the numberX is null") + +/*Add a console.log statement that logs the value of numberX.*/ +console.log(numberX) + +/*Now initialize your variable numberX with a number (also called an integer in computer science terms).*/ +numberX=5 + +/*Next, add a console.log statement that explains what you think the value of numberX is.*/ +console.log("Now the numberX has a value") + +/*Add a console.log statement that logs the value of numberX.*/ +console.log(numberX) \ No newline at end of file diff --git a/Week1/homework/js-exercises/4_LogString.js b/Week1/homework/js-exercises/4_LogString.js new file mode 100644 index 000000000..f6c3341db --- /dev/null +++ b/Week1/homework/js-exercises/4_LogString.js @@ -0,0 +1,18 @@ +'use strict' +/*Declare a variable myString and assign a string to it. Use your full name, including spaces, as the content for the string.*/ +let myString="Kiriakos Mouzakis"; + +/*Write a console.log statement in which you explain in words what you think the value of the string is.*/ +console.log("The value of myString is my full name"); + +/*Now console.log the variable myString.*/ +console.log(myString); + +/*Now reassign to the variable myString a new string.*/ +myString=("Now I am Batman"); + +/*Just like what you did before write a console.log statement that explains in words what you think will be logged to the console.*/ +console.log("Now the string has changed"); + +/*Now console.log myString again.*/ +console.log(myString); diff --git a/Week1/homework/js-exercises/5_RndNum.js b/Week1/homework/js-exercises/5_RndNum.js new file mode 100644 index 000000000..07916021f --- /dev/null +++ b/Week1/homework/js-exercises/5_RndNum.js @@ -0,0 +1,23 @@ +'use strict' +/*Declare a variable z and assign the number 7.25 to it.*/ +let z=7.25; + +/*Write a console.log statement in which you log the value of z.*/ +console.log("'Z' value is "+z); + +/*Declare another variable a that has the value of z but rounded to the nearest integer.*/ +let a; +a=Math.round(z); + +/*Write a console.log statement in which you log the value of a.*/ +console.log("'A' has the same value but is rounded "+a); + +/*So now we have z and a find a way to compare the two values and store the highest of the two in a new variable. */ +/*Write a console.log statement in which you log the value of the highest value.*/ +function bigger(){ + if(a>z) {console.log("'A' is bigger with value of "+a) + } + else {console.log("'Z' is bigger with value of "+z) + } +} +bigger() \ No newline at end of file diff --git a/Week1/homework/js-exercises/6_arrayAnimals.js b/Week1/homework/js-exercises/6_arrayAnimals.js new file mode 100644 index 000000000..deb47a15c --- /dev/null +++ b/Week1/homework/js-exercises/6_arrayAnimals.js @@ -0,0 +1,23 @@ +'use strict' +/*Declare variable and assign to it an empty array. +Make sure that the name of the variable indicates it contains more than 1 item. For example items instead of item.*/ +let array1=new Array; + +/*Write a console.log statement that explains in words what you think the value of the array is.*/ +console.log("The value of the array is empty") + +/*Write a console.log statement that logs the array.*/ +console.log(array1); + +/*Create a new variable with an array that has 3 of your favorite animals, each in a different string. +Make sure the name of the variables says something about what the variable contains.*/ +let animals=["lion","tiger","panther"]; + +/*Write a console.log statement that logs the second array.*/ +console.log(animals); + +/*Add a statement that adds another string ("Piglet)" to the array of animals.*/ +animals.push("Piglet"); + +/*Write a console.log statement that logs the second array!*/ +console.log(animals); \ No newline at end of file diff --git a/Week1/homework/js-exercises/7_stringLength.js b/Week1/homework/js-exercises/7_stringLength.js new file mode 100644 index 000000000..da5138ec0 --- /dev/null +++ b/Week1/homework/js-exercises/7_stringLength.js @@ -0,0 +1,9 @@ +'use strict' +/*/Declare a variable called mySentence and initialize it with the following string: "Programming is so interesting!".*/ +let mySentence="Programming is so interesting!"; + +/*Figure out (using Google) how to get the length of mySentence.*/ +let stringLength=mySentence.length; + +/*Write a console.log statement to log the length of mySentence.*/ +console.log(stringLength); \ No newline at end of file diff --git a/Week1/homework/js-exercises/8_typeChecker.js b/Week1/homework/js-exercises/8_typeChecker.js new file mode 100644 index 000000000..895098bb0 --- /dev/null +++ b/Week1/homework/js-exercises/8_typeChecker.js @@ -0,0 +1,37 @@ +'use strict' +/*Write a program that checks the data types of two variables and logs to the console SAME TYPE if they are the same type. +If they are different types log Not the same....*/ +/*Declare 4 variables: 2 must be strings and 2 must be objects*/ +let string1="social"; +let string2="hackers"; +let fname={firstname:"kiriakos"}; +let lname={lastname:"mouzakis"}; + +/*Create 6 conditional statements, where for each you check if the data type of one variable is the same as the other*/ +if (typeof string1 === typeof string2) {console.log("string1 and string2 are the same type");} +if (typeof string1 === typeof fname) {console.log("string1 and fname are the same type");} +if (typeof string1 === typeof lname) {console.log("string1 and lname are the same type");} +if (typeof string2 === typeof fname) {console.log("string2 and fname are the same type");} +if (typeof string2 === typeof lname) {console.log("string2 and lname are the same type");} +if (typeof fname === typeof lname) {console.log("fname and lname are the same type");} + +/*Find out how to check the type of a variable*/ +console.log("You can check a type of a variable with the typeof() command eg.. The type of fname is an "+typeof fname); + +/*Write 2 console.log statements to log the type of 2 variables, each with a different data type*/ +console.log(typeof(string1)); +console.log(typeof(lname)); + +/*Now compare the types of your different variables with one another*/ +/*Log Not the same... when the types are different*/ +let num1=1; +if (typeof string1 === typeof string2) {console.log("string1 and string2 are the same type")} else {console.log("Not the same type"); } +if (typeof string1 === typeof fname) {console.log("string1 and fname are the same type");} else {console.log("Not the same type"); } +if (typeof string1 === typeof lname) {console.log("string1 and lname are the same type");} else {console.log("Not the same type"); } +if (typeof string2 === typeof fname) {console.log("string2 and fname are the same type");} else {console.log("Not the same type"); } +if (typeof string2 === typeof lname) {console.log("string2 and lname are the same type");} else {console.log("Not the same type"); } +if (typeof fname === typeof lname) {console.log("fname and lname are the same type");} else {console.log("Not the same type"); } +if (typeof num1 === typeof string1) {console.log("num1 and string1 are the same type");} else {console.log("Not the same type"); } +if (typeof num1 === typeof string2) {console.log("num1 and string2 are the same type");} else {console.log("Not the same type"); } +if (typeof num1 === typeof fname) {console.log("num1 and fname are the same type");} else {console.log("Not the same type"); } +if (typeof num1 === typeof lname) {console.log("num1 and lname are the same type");} else {console.log("Not the same type"); } \ No newline at end of file diff --git a/Week1/homework/js-exercises/9_remainder.js b/Week1/homework/js-exercises/9_remainder.js new file mode 100644 index 000000000..0abce4e30 --- /dev/null +++ b/Week1/homework/js-exercises/9_remainder.js @@ -0,0 +1,9 @@ +'use strict' +/*For each of these, write in comments what the answer is followed by how you came to that conclusion*/ + +/*If x equals 7, and the only other statement is x = x % 3, what would be the value of x after the calculation?*/ +console.log("At first x will have the value 7, then after the division it will take the value of the modulus (% symbol) and will be 1"); +/*If y equals 21, and the only other statement is y = y % 4, what would be the value of y after the calculation?*/ +console.log("At first y will have the value 21, then after the division it will take the value of the modulus (% symbol) and will be 1"); +/*If z equals 13, and the only other statement is z = z % 2, what would be the value of z after the calculation?*/ +console.log("At first z will have the value 13, then after the division it will take the value of the modulus (% symbol) and will be 1"); diff --git a/Week2/homework/js1_week2_exercises/1_removeComma.js b/Week2/homework/js1_week2_exercises/1_removeComma.js new file mode 100644 index 000000000..e90473ef4 --- /dev/null +++ b/Week2/homework/js1_week2_exercises/1_removeComma.js @@ -0,0 +1,13 @@ +'use strict' +/*let myString = "hello,this,is,a,difficult,to,read,sentence"; +Add the variable to your file.*/ +let myString = "hello,this,is,a,difficult,to,read,sentence" + +//Log the length of myString. +console.log(myString.length); + +//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!) +let newString=myString.replace(/,/g,' '); + +//After replacing the commas, log myString to see if you succeeded. +console.log(newString); \ No newline at end of file diff --git a/Week2/homework/js1_week2_exercises/2_oddEven.js b/Week2/homework/js1_week2_exercises/2_oddEven.js new file mode 100644 index 000000000..67498763c --- /dev/null +++ b/Week2/homework/js1_week2_exercises/2_oddEven.js @@ -0,0 +1,12 @@ +'use strict' +/*Report whether or not a number is odd/even! +Create a for loop, that iterates from 0 to 20.*/ + +for(let i=0; i<21; i++){ + /*Create a conditional statement that checks if the value of the counter variable is odd or even. + If it's odd, log to the console The number [PUT_NUMBER_HERE] is odd!. + If it's even, log to the console The number [PUT_NUMBER_HERE] is even!.*/ + if(i%2) { + console.log("The number "+i+" is odd."); + } + else console.log("The number "+i+" is even.");} diff --git a/Week2/homework/js1_week2_exercises/3_recipeCard.js b/Week2/homework/js1_week2_exercises/3_recipeCard.js new file mode 100644 index 000000000..29d8af90b --- /dev/null +++ b/Week2/homework/js1_week2_exercises/3_recipeCard.js @@ -0,0 +1,18 @@ +'use strict' +/*Ever wondered how to make a certain meal? Let's create a recipe list with JavaScript! +Declare a variable that holds an object (your meal recipe).*/ +//Give the object 3 properties: a title (string), a servings (number) and an ingredients (array of strings) property. +const tost={ + Title:"Tost", + Servings:1, + Ingredients:["Slices of bread","cheese","ham","tomato"] +} +//Log each property out seperately, using a loop (for, while or do/while) + +let recipe=[tost] + +for (let i of recipe){ + console.log("Title: "+i.Title); + console.log("Servings: "+i.Servings); + console.log("Ingredients: "+"\n"+i.Ingredients[0]+"\n"+i.Ingredients[1]+"\n"+i.Ingredients[2]+"\n"+i.Ingredients[3]); +} diff --git a/Week2/homework/js1_week2_exercises/4_readingList.js b/Week2/homework/js1_week2_exercises/4_readingList.js new file mode 100644 index 000000000..184702765 --- /dev/null +++ b/Week2/homework/js1_week2_exercises/4_readingList.js @@ -0,0 +1,40 @@ +'use strict' +/*Keep track of which books you read and which books you want to read! +Declare a variable that holds an array of 3 objects, where each object describes a book and +has properties for the title (string), author (string), and alreadyRead (boolean indicating if you read it yet).*/ +const lord_of_the_rings={ + title:"The Fellowship of the Ring", + author:"J. R. R. Tolkien", + alreadyRead:false +} +const game_of_thrones={ + title:"A Song of Ice and Fire", + author:"George R. R. Martin", + alreadyRead:true +} +const harry_potter={ + title:"The Philosopher's Stone", + author:"J. K. Rowling", + alreadyRead:true +} +//Loop through the array of books. +var myBooks=[lord_of_the_rings,game_of_thrones,harry_potter]; + +myBooks.forEach(item=> console.log(item)); + +//For each book, log the book title and book author like so: "The Hobbit by J.R.R. Tolkien". +myBooks.forEach(item=> { + return console.log(item.title + " by " + item.author+ "\n"); +}); + +/*Create a conditional statement to change the log depending on whether you read it yet or not. +If you read it, log a string like You already read "The Hobbit" right after the log of the book details +If you haven't read it log a string like You still need to read "The Lord of the Rings"*/ +for (let item of myBooks){ + if (item.alreadyRead == true){ + + console.log("You already read: " + item.title + "\n"); +}else{ + console.log("You still nead to read: "+ item.title + "\n"); +} +} \ No newline at end of file diff --git a/Week2/homework/js1_week2_exercises/5_drinkTray.js b/Week2/homework/js1_week2_exercises/5_drinkTray.js new file mode 100644 index 000000000..f3718723e --- /dev/null +++ b/Week2/homework/js1_week2_exercises/5_drinkTray.js @@ -0,0 +1,21 @@ +'use strict' +//You're at a party and you feel thirsty! However, you've got 5 friends who are also in need of a drink. Let's go get them a drink. +//Declare a variable that holds an empty array, called drinkTray. +let drinkTray=new Array(5); + +//There are 3 different types of drinks: +//const drinkTypes = ["cola", "lemonade", "water"]; +const drinkTypes = ["cola", "lemonade", "water"]; + +/*Create a loop that runs 5 times. On each iteration, push a drink into the drinkTray variable. +The drinkTray can only hold at most two instances of the same drink type, for example it can only hold 2 colas, 2 lemonades, 2 waters. +If there are already two instances of a drinkType then start with the next drink in the array. +Your drinkTray should contain 2 cola, 2 lemonade and 1 water.*/ +let x = 0; +for (let i = 0; i < 5; i++){ + drinkTray[i] = drinkTypes[x]; + if(i%2 == 1) x++; +} + +//Log to the console: "Hey guys, I brought a [INSERT VALUES FROM ARRAY]!" (For example: "Hey guys, I brought a cola, cola, lemonade, lemonade, water!") +console.log("Hey guys, I brought a " + drinkTray.join()+"."); \ No newline at end of file diff --git a/Week2/homework/project_gradeCalc/gradeCalc.js b/Week2/homework/project_gradeCalc/gradeCalc.js new file mode 100644 index 000000000..c5a92e174 --- /dev/null +++ b/Week2/homework/project_gradeCalc/gradeCalc.js @@ -0,0 +1,45 @@ +'use strict' +/*In this project you'll write a function that calculates grades, +based on the American grading system! Let's say a student did a test and they got a 60 out of 100, this function will: +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 +In this example this is what we would expect the function to return in the command line: + +You got a D (60%)! +When writing the function, make use of the following grade scores: + +Grade A (90% - 100%) +Grade B (80% - 89%) +Grade C (70% - 79%) +Grade D (60% - 69%) +Grade E (50% - 59%) +Grade F (0% - 49%)*/ + + + +function grade(score){ + if(score >= 90 && score <= 100){ + console.log(`You got a A (${score}%)!`) + } + else if(score >= 80 && score <= 89){ + console.log(`You got a B (${score}%)!`) + } + else if(score >= 70 && score <= 79){ + console.log(`You got a C (${score}%)!`) + } + else if(score >= 60 && score <= 69){ + console.log(`You got a D (${score}%)!`) + } + else if(score >= 50 && score <= 59){ + console.log(`You got a E (${score}%)!`) + } + else if(score >= 0 && score <= 49){ + console.log(`You got a F (${score}%)!`) + } +} + +grade(Math.floor(Math.random() * 100)); + + + diff --git a/Week3/homework/credit card project/creditCardValidator.js b/Week3/homework/credit card project/creditCardValidator.js new file mode 100644 index 000000000..bfacf1ddb --- /dev/null +++ b/Week3/homework/credit card project/creditCardValidator.js @@ -0,0 +1,60 @@ +'use strict'; + +function cardValidator(number) { + var number; + let even = number.match(/^(d*[0-9]){15}[02468]/gs); //Takes 15 digits and the last one must be even number, if not returns null. + let sameNum= number.match(/^(\d)(?!\1+$)\d{15}$/gs); //Checks if the numbers are the same, if not returns null. + + function overSixteen() { //Function that checks if the sum off all numbers are over 16, if not returns null. + let sum = 0; + for (let i = 0; i < number.length; i++) { + sum += parseInt(number[i]); + } + if(sum<16){ + return null; + } + } + + if(even && sameNum && overSixteen != null) { + console.log("Valid card: "+number); + } + else + console.log("Invalid card: "+number) + } + +cardValidator("1234567891234566"); +cardValidator("6666666666661666"); +cardValidator("a92332119c011112"); +cardValidator("4444444444444444"); +cardValidator("1111111111111110"); +cardValidator("6666666666666661"); + + +/*In this project you'll write a script that validates whether or not a credit card number is valid. + +Here are the rules for a valid number: + +- 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: + +9999777788880000 +6666666666661666 + +The following credit card numbers are invalid: + +a92332119c011112 (invalid characters) +4444444444444444 (only one type of number) +1111111111111110 (sum less than 16) +6666666666666661 (odd final number) + +These are the requirements your project needs to fulfill: + +- Make a JavaScript file with a name that describes its contents +- Create a function with a descriptive name, for example: `doSomething` or `calcAnotherThing` +- Write at least 2 comments that explain to others what a line of code is meant to do +- Make the return value of the function a template string, so you can insert variables! +- Use `node` from the command line to test if your code works as expected +*/ \ No newline at end of file diff --git a/Week3/homework/js-exercises/1_compliment.js b/Week3/homework/js-exercises/1_compliment.js new file mode 100644 index 000000000..489850655 --- /dev/null +++ b/Week3/homework/js-exercises/1_compliment.js @@ -0,0 +1,21 @@ +'use strict' + +//1. Write a function named `giveCompliment` + function giveCompliment(name){ + +/*- 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]!*/ + + let compliment=["great","good","awesome","the best","excellent","superb","amazing","flawless","victorious","the man"]; + const randomCompliment = compliment[Math.floor(Math.random() * compliment.length)]; + return console.log(`You are ${randomCompliment},${name}!`) + } +//2. Call the function three times, giving each function call the same argument: your name. + for(let i=0; i<3; i++){ + giveCompliment("Kiriako") + } + + + diff --git a/Week3/homework/js-exercises/2_dogYears.js b/Week3/homework/js-exercises/2_dogYears.js new file mode 100644 index 000000000..acac06540 --- /dev/null +++ b/Week3/homework/js-exercises/2_dogYears.js @@ -0,0 +1,21 @@ +'use strict' +//You know how old your dog is in human years, but what about dog years? Calculate it! + +/*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!"*/ +function calculateDogAge(number){ + let dogAge=number*7; + + console.log(`Your doggie is ${dogAge} years old in dog years!`) +} + +//2. Call the function three times with different sets of values.*/ + +for(let i=0; i<3; i++){ + function getRandomInt(max) { + return Math.floor(Math.random() * Math.floor(max)); + } + calculateDogAge(getRandomInt(15)); +} \ No newline at end of file diff --git a/Week3/homework/js-exercises/3_fortuneTeller.js b/Week3/homework/js-exercises/3_fortuneTeller.js new file mode 100644 index 000000000..77c59d444 --- /dev/null +++ b/Week3/homework/js-exercises/3_fortuneTeller.js @@ -0,0 +1,43 @@ +'use strict' +//Why pay a fortune teller when you can just program your fortune yourself? +/*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*/ + +let partnerNames=[ + "Maria", + "Anna", + "Jeniffer", + "Penelope", + "Eleftheria"]; + +let locations=[ + "Athens", + "New York", + "Tokyo", + "Berlin"]; + +let jobs=[ + "doctor", + "lawer", + "police-officer", + "electrician", + "farmer"]; + +function tellFortune(randomJobs,randomLocations,randomPartner,numChildren){ + + numChildren=[getRandomInt(5)]; + function getRandomInt(max) { + return Math.floor(Math.random() * Math.floor(max)); + } + randomPartner = partnerNames[Math.floor(Math.random() * partnerNames.length)]; + randomLocations = locations[Math.floor(Math.random() * locations.length)]; + randomJobs = jobs[Math.floor(Math.random() * jobs.length)]; + + return(`You will be a ${randomJobs} in ${randomLocations}, and married to ${randomPartner} with ${numChildren} kids.`) +} + +//3. Call the function 1 time, by passing the arrays as the argument.*/ +console.log(tellFortune()) \ No newline at end of file diff --git a/Week3/homework/js-exercises/4_superMarket.js b/Week3/homework/js-exercises/4_superMarket.js new file mode 100644 index 000000000..1c74d3f6a --- /dev/null +++ b/Week3/homework/js-exercises/4_superMarket.js @@ -0,0 +1,27 @@ +'use strict' +/*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"`*/ + +let shoppingItems=[ + "bananas", + "milk" +] +function addToShoppingCart(product){ + if(shoppingItems.length > 2) + shoppingItems.shift() + shoppingItems.push(product) + return(`You bought ${shoppingItems}!`) +} + +//3. Call the function 3 times, each time with a different string as the argument. +console.log(addToShoppingCart("bacon")); +console.log(addToShoppingCart("salmon")); +console.log(addToShoppingCart("beef")); + + + diff --git a/Week3/homework/js-exercises/5_totalCost.js b/Week3/homework/js-exercises/5_totalCost.js new file mode 100644 index 000000000..881cad044 --- /dev/null +++ b/Week3/homework/js-exercises/5_totalCost.js @@ -0,0 +1,27 @@ +'use strict' +/*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`)*/ +function calculateTotalPrice(cartForParty){ + cartForParty={ + beer:1.75, + chips:0.99, + coca_cola:3.90, + pizza:6.70, + party_cake:15.70 + } + let total = 0; + for (let property in cartForParty) { + total += cartForParty[property]; +} + return total; +} +//3. Call the function 1 time, giving it the object `cartForParty` as an argument*/ +console.log("Total price: "+calculateTotalPrice()+"€"); + +