From ca172cbe8b9205d0219614cbf676723cdb7a2785 Mon Sep 17 00:00:00 2001 From: Nabi Abudaldah Date: Wed, 5 Aug 2020 15:11:47 +0200 Subject: [PATCH] Created exercise templates --- Week1/exercise01.js | 23 +++++++++++++++++++++++ Week1/exercise02.js | 21 +++++++++++++++++++++ Week1/exercise03.js | 17 +++++++++++++++++ Week1/exercise04.js | 17 +++++++++++++++++ Week1/exercise05.js | 17 +++++++++++++++++ Week1/exercise06.js | 18 ++++++++++++++++++ Week1/exercise07.js | 14 ++++++++++++++ Week1/exercise08.js | 36 ++++++++++++++++++++++++++++++++++++ Week1/exercise09.js | 14 ++++++++++++++ Week1/exercise10.js | 17 +++++++++++++++++ Week2/exercise01.js | 19 +++++++++++++++++++ Week2/exercise02.js | 15 +++++++++++++++ Week2/exercise03.js | 19 +++++++++++++++++++ Week2/exercise04.js | 18 ++++++++++++++++++ Week2/exercise05.js | 24 ++++++++++++++++++++++++ Week2/project/main.js | 34 ++++++++++++++++++++++++++++++++++ Week3/exercise01.js | 21 +++++++++++++++++++++ Week3/exercise02.js | 20 ++++++++++++++++++++ Week3/exercise03.js | 26 ++++++++++++++++++++++++++ Week3/exercise04.js | 24 ++++++++++++++++++++++++ Week3/exercise05.js | 21 +++++++++++++++++++++ Week3/project/main.js | 37 +++++++++++++++++++++++++++++++++++++ 22 files changed, 472 insertions(+) create mode 100644 Week1/exercise01.js create mode 100644 Week1/exercise02.js create mode 100644 Week1/exercise03.js create mode 100644 Week1/exercise04.js create mode 100644 Week1/exercise05.js create mode 100644 Week1/exercise06.js create mode 100644 Week1/exercise07.js create mode 100644 Week1/exercise08.js create mode 100644 Week1/exercise09.js create mode 100644 Week1/exercise10.js create mode 100644 Week2/exercise01.js create mode 100644 Week2/exercise02.js create mode 100644 Week2/exercise03.js create mode 100644 Week2/exercise04.js create mode 100644 Week2/exercise05.js create mode 100644 Week2/project/main.js create mode 100644 Week3/exercise01.js create mode 100644 Week3/exercise02.js create mode 100644 Week3/exercise03.js create mode 100644 Week3/exercise04.js create mode 100644 Week3/exercise05.js create mode 100644 Week3/project/main.js diff --git a/Week1/exercise01.js b/Week1/exercise01.js new file mode 100644 index 000000000..6476b6f14 --- /dev/null +++ b/Week1/exercise01.js @@ -0,0 +1,23 @@ +'use strict' + +/* + +Exercise 1: Hello world! + +Write a statement, using the console.log() function. It should fulfill the following requirements: + +It takes a string as an argument +The string should contain the message "Hello world!" +Execute the function 10 times, each time using the phrase in different languages +For example: + +Halo, dunia! // Indonesian +Ciao, mondo! // Italian +Hola, mundo! // Spanish +Finished? Using the command line, navigate to your js-exercises folder and type in the following to test your code: + +foo@bar:~$ node exercise01.js +Expected output: It should show the message Hello world! in 10 different languages. + +*/ + diff --git a/Week1/exercise02.js b/Week1/exercise02.js new file mode 100644 index 000000000..831f8ce07 --- /dev/null +++ b/Week1/exercise02.js @@ -0,0 +1,21 @@ +'use strict' + +/* + +Exercise 2: Error debugging + +Consider the following code: + +console.log('I'm awesome'!; +Here are the requirements: + +Copy the code in your .js file and run it in the command line using node. +You will see that you will get a SyntaxError. + +Correct the mistake. +Hint: the SyntaxError message will give you some indication of what the error might be, but figure out yourself how to correct it! + +Expected output: When done right, the command line should show the message I'm awesome!. + +*/ + diff --git a/Week1/exercise03.js b/Week1/exercise03.js new file mode 100644 index 000000000..162d1b320 --- /dev/null +++ b/Week1/exercise03.js @@ -0,0 +1,17 @@ +'use strict' + +/* + +Exercise 3: Log the number + +Follow the steps. Make sure that each step is written on the line after. + +First, declare your variable numberX. Do not initialize it (which means, don't give it a starting value) yet +Add a console.log statement that explains in words what you think the value of x is +Add a console.log statement that logs the value of numberX. +Now initialize your variable numberX with a number (also called an integer in computer science terms) +Next, add a console.log statement that explains what you think the value of numberX is +Add a console.log statement that logs the value of numberX + +*/ + diff --git a/Week1/exercise04.js b/Week1/exercise04.js new file mode 100644 index 000000000..b81afb0ff --- /dev/null +++ b/Week1/exercise04.js @@ -0,0 +1,17 @@ +'use strict' + +/* + +Exercise 4: Log the string + +Follow the steps. Make sure that each step is written on the line after. + +Declare a variable myString and assign a string to it. Use your full name, including spaces, as the content for the string. +Write a console.log statement in which you explain in words what you think the value of the string is. +Now console.log the variable myString. +Now reassign to the variable myString a new string. +Just like what you did before write a console.log statement that explains in words what you think will be logged to the console. +Now console.log myString again. + +*/ + diff --git a/Week1/exercise05.js b/Week1/exercise05.js new file mode 100644 index 000000000..d08b5bea9 --- /dev/null +++ b/Week1/exercise05.js @@ -0,0 +1,17 @@ +'use strict' + +/* + +Exercise 5: Round a number and log it + +Follow the steps. Make sure that each step is written on the line after. + +Declare a variable z and assign the number 7.25 to it. +Write a console.log statement in which you log the value of z. +Declare another variable a that has the value of z but rounded to the nearest integer. +Write a console.log statement in which you log the value of 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. + +*/ + diff --git a/Week1/exercise06.js b/Week1/exercise06.js new file mode 100644 index 000000000..6f9834ea7 --- /dev/null +++ b/Week1/exercise06.js @@ -0,0 +1,18 @@ +'use strict' + +/* + +Exercise 6: Log an array of animals + +Follow the steps. Make sure that each step is written on the line after. + +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. +Write a console.log statement that explains in words what you think the value of the array is. +Write a console.log statement that logs the array. +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. +Write a console.log statement that logs the second array. +Add a statement that adds another string ("Piglet)" to the array of animals. +Write a console.log statement that logs the second array! + +*/ + diff --git a/Week1/exercise07.js b/Week1/exercise07.js new file mode 100644 index 000000000..f112527b4 --- /dev/null +++ b/Week1/exercise07.js @@ -0,0 +1,14 @@ +'use strict' + +/* + +Exercise 7: Log the length of a string + +Follow the steps. Make sure that each step is written on the line after. + +Declare a variable called mySentence and initialize it with the following string: "Programming is so interesting!". +Figure out (using Google) how to get the length of mySentence. +Write a console.log statement to log the length of mySentence. + +*/ + diff --git a/Week1/exercise08.js b/Week1/exercise08.js new file mode 100644 index 000000000..04aa654d1 --- /dev/null +++ b/Week1/exercise08.js @@ -0,0 +1,36 @@ +'use strict' + +/* + +Exercise 8: Type checker + +Create a function that fulfills the following requirements: + +Takes in 2 arguments +Check the data type of each +Compares each data type +Logs to the console the message SAME TYPE if they are the same type. If they are different types log Not the same.... +Follow the steps: + +Declare 4 variables: 2 must be strings and 2 must be objects +Create 6 conditional statements, where for each you check if the data type of one variable is the same as the other +Find out how to check the type of a variable +Write 2 console.log statements to log the type of 2 variables, each with a different data type +Now compare the types of your different variables with one another +Log Not the same... when the types are different +Here's an incomplete example of how it could look: + +// Declare all variables +let x = 9; +let y = 67; + +// Check data type +console.log(...); + +// Check if data type is the same +if (...) { + console.log('SAME TYPE'); +} + +*/ + diff --git a/Week1/exercise09.js b/Week1/exercise09.js new file mode 100644 index 000000000..f08b4e99a --- /dev/null +++ b/Week1/exercise09.js @@ -0,0 +1,14 @@ +'use strict' + +/* + +Exercise 9: Log the remainder + +Answer the following questions. For each, 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? +If y equals 21, and the only other statement is y = y % 4, what would be the value of y after the calculation? +If z equals 13, and the only other statement is z = z % 2, what would be the value of z after the calculation? + +*/ + diff --git a/Week1/exercise10.js b/Week1/exercise10.js new file mode 100644 index 000000000..9cdf0cda5 --- /dev/null +++ b/Week1/exercise10.js @@ -0,0 +1,17 @@ +'use strict' + +/* + +Exercise 10: Compare arrays + +Follow the steps: + +Declare 2 variables, that each hold an array. The first array should have 4 items, the second 7 items +Find out how to get the length of each array. Write a console.log statement that shows the length of each array +const array = ["hello", 123, true, { name: "Noer" }]; + +console.log('The length of the array is...' + ...); +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 + +*/ + diff --git a/Week2/exercise01.js b/Week2/exercise01.js new file mode 100644 index 000000000..d4988a72b --- /dev/null +++ b/Week2/exercise01.js @@ -0,0 +1,19 @@ +'use strict' + +/* + +Exercise 1: Remove the comma + +Consider the following string: + +let myString = 'hello,this,is,a,difficult,to,read,sentence'; +Follow the steps: + +Add the variable to your file. +Log the length of myString. +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!) +After replacing the commas, log myString to see if you succeeded. +Expected result in the console: hello this is a difficult to read sentence + +*/ + diff --git a/Week2/exercise02.js b/Week2/exercise02.js new file mode 100644 index 000000000..40e799503 --- /dev/null +++ b/Week2/exercise02.js @@ -0,0 +1,15 @@ +'use strict' + +/* + +Exercise 2: The even/odd reporter + +Report whether or not a number is odd/even! + +Create a for loop, that iterates from 0 to 20. +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!. + +*/ + diff --git a/Week2/exercise03.js b/Week2/exercise03.js new file mode 100644 index 000000000..2f530c8bb --- /dev/null +++ b/Week2/exercise03.js @@ -0,0 +1,19 @@ +'use strict' + +/* + +Exercise 3: The recipe card + +Ever wondered how to make a certain meal? Let's create a recipe list with JavaScript! + +Declare a variable that holds an empty object literal (your meal recipe). +Give the object 3 properties: a title (string), a servings (number) and an ingredients (array of strings) property. +Log each property out separately, using a loop (for, while or do/while) +Expected result: + +Meal name: Omelette +Serves: 2 +Ingredients: 4 eggs, 2 strips of bacon, 1 tsp salt/pepper + +*/ + diff --git a/Week2/exercise04.js b/Week2/exercise04.js new file mode 100644 index 000000000..6e4b6d62f --- /dev/null +++ b/Week2/exercise04.js @@ -0,0 +1,18 @@ +'use strict' + +/* + +Exercise 4: The reading list + +Keep track of which books you read and which books you want to read! + +Follow the steps: + +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). +Loop through the array of books. +For each book, log the book title and book author like so: "The Hobbit by J.R.R. Tolkien". +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" + +*/ + diff --git a/Week2/exercise05.js b/Week2/exercise05.js new file mode 100644 index 000000000..224662500 --- /dev/null +++ b/Week2/exercise05.js @@ -0,0 +1,24 @@ +'use strict' + +/* + +Exercise 5: Who wants a drink? + +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. +There are 3 different types of drinks: + +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. +// Expected result: +const drinkTray = ['cola', 'cola', 'lemonade', 'lemonade', 'water']; + +// 'Hey guys, I brought a cola, cola, lemonade, lemonade, water!' +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. +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!") +Test out your code using node in the command line! + +*/ + diff --git a/Week2/project/main.js b/Week2/project/main.js new file mode 100644 index 000000000..93dff9cb1 --- /dev/null +++ b/Week2/project/main.js @@ -0,0 +1,34 @@ +'use strict' + +/* + +4. PROJECT: Grade calculator +Every week ends with a project you have to build on your own. Instead of getting clear-cut instructions, you'll get a list of criteria that your project needs to measure up to. + +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: + +convert the score into a percentage +calculate what grade corresponds with that percentage, and +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%) +These are the requirements your project needs to fulfill: + +Make a JavaScript file with a name that describes its contents +Use either a switch or if/else statement +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 +Good luck! + +*/ + diff --git a/Week3/exercise01.js b/Week3/exercise01.js new file mode 100644 index 000000000..6bfb72495 --- /dev/null +++ b/Week3/exercise01.js @@ -0,0 +1,21 @@ +'use strict' + +/* + +Exercise 1: You are amazing, Noer! + +There is no better way to start your day then with a compliment! + +Write a function named giveCompliment +It takes 1 argument: your name +Inside the function define a variable that holds an array, compliments, with 10 strings. Each string should be a compliment, like "great", "awesome" +Write code that randomly selects a compliment +Return a string: "You are [COMPLIMENT], [YOUR_NAME]! +Call the function three times, giving each function call the same argument: your name. +// Expected output +giveCompliment('Noer'); // Returns "You are amazing, Noer!" +giveCompliment('Noer'); // Returns "You are great, Noer!" +giveCompliment('Noer'); // Returns "You are supersmart, Noer!" + +*/ + diff --git a/Week3/exercise02.js b/Week3/exercise02.js new file mode 100644 index 000000000..9a90d7dec --- /dev/null +++ b/Week3/exercise02.js @@ -0,0 +1,20 @@ +'use strict' + +/* + +Exercise 2: Dog years + +You know how old your dog is in human years, but what about dog years? Let's calculate it! + +Write a function named calculateDogAge. +It takes 1 argument: your (fictional) 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!" +Call the function three times with different sets of values. +// Expected output +calculateDogAge(1); // Returns "Your doggie is 7 years old in dog years!" +calculateDogAge(2); // Returns "Your doggie is 14 years old in dog years!" +calculateDogAge(3); // Returns "Your doggie is 21 years old in dog years!" + +*/ + diff --git a/Week3/exercise03.js b/Week3/exercise03.js new file mode 100644 index 000000000..d4e454dc4 --- /dev/null +++ b/Week3/exercise03.js @@ -0,0 +1,26 @@ +'use strict' + +/* + +Exercise 3: Be your own fortune teller + +Why pay a fortune teller when you can just program your fortune yourself? + +Create 4 arrays, numChildren, partnerNames, locations and jobs. Give each array 5 random string values that have to do with the name of the variable +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], married to [PARTNER_NAME] with [NUMBER_KIDS] kids." +Call the function 3 times, by passing the arrays as the argument. +// Expected output +const numChildren = [...]; +const partnerNames = [...]; +const locations = [...]; +const jobs = [...]; + +tellFortune(numChildren, partnerNames, locations, jobs); // Returns "You will be a programmer in The Netherlands, married to Layla with 3 kids." +tellFortune(numChildren, partnerNames, locations, jobs); // Returns "You will be a farmer in France, married to Muhammed with 2 kids." +tellFortune(numChildren, partnerNames, locations, jobs); // Returns "You will be a teacher in The United States, married to Zara with 4 k + +*/ + diff --git a/Week3/exercise04.js b/Week3/exercise04.js new file mode 100644 index 000000000..98cdd0017 --- /dev/null +++ b/Week3/exercise04.js @@ -0,0 +1,24 @@ +'use strict' + +/* + +Exercise 4: Shopping at the supermarket + +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. + +Create an array called shoppingCart that holds the following strings: "bananas" and "milk" +Write a function named addToShoppingCart +It takes 1 argument: a grocery item (string) +Add grocery item to shoppingCart. If the amount of items is more than 3 remove the first one in the array +Loops through the array in order to list out the items +Return a string: "You bought [LIST_OF_GROCERY_ITEMS]!" +Call the function 3 times, each time with a different string as the argument. +const shoppingCart = ['bananas', 'milk']; + +// Expected output +addToShoppingCart('chocolate'); // Returns "You bought bananas, milk, chocolate!" +addToShoppingCart('waffles'); // Returns "You bought milk, chocolate, waffles!" +addToShoppingCart('tea'); // Returns "You bought chocolate, waffles, tea!" + +*/ + diff --git a/Week3/exercise05.js b/Week3/exercise05.js new file mode 100644 index 000000000..eaf42ad2d --- /dev/null +++ b/Week3/exercise05.js @@ -0,0 +1,21 @@ +'use strict' + +/* + +Exercise 5: Total cost is ... + +You want to buy a couple of things from the supermarket to prepare for a party. After scanning all the items the cashier wants to give you the total price, but the machine is broken! Let's write her a function that does it for her instead! + +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) +Write a function called calculateTotalPrice +It takes 1 argument: an object that contains properties that only contain number values +Loop through the object and add all the number values together +Return a string: "Total: €[TOTAL_PRICE_ITEMS]" +Call the function 1 time, giving it the object cartForParty as an argument +const cartForParty = { ... } + +// Expected output +calculateTotalPrice(cartForParty); // Returns "Total: €10.75" + +*/ + diff --git a/Week3/project/main.js b/Week3/project/main.js new file mode 100644 index 000000000..25e963be1 --- /dev/null +++ b/Week3/project/main.js @@ -0,0 +1,37 @@ +'use strict' + +/* + +4. PROJECT: Credit Card Validator +Every week ends with a project you have to build on your own. Instead of getting clear-cut instructions, you'll get a list of criteria that your project needs to measure up to. + +In this project you'll write a function, called validateCreditNumber, that validates whether or not a credit card number is valid. + +Here are the criteria for a valid number: + +Input must be 16 characters +All characters must be numbers +At least two different numbers should be represented +The last number must be even +The sum of all the numbers 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) +// Expected output + +validateCreditNumber('a92332119c011112'); // Returns "Invalid! The input a92332119c011112 should contain only numbers! +validateCreditNumber('4444444444444444'); // Returns "Invalid! The input 4444444444444444 should contain at least 2 different types of numbers! +validatecreditNumber('6666666666661666'); // Returns "Success! The input 6666666666661666 is a valid credit card number! +After you've written your function, use node in the command line to test to see if your code works as expected. + +Good luck! + +*/ +