From 6aa18bc8a8d257e0985f03806c93ceaf48fb1120 Mon Sep 17 00:00:00 2001 From: Tarek <2b3zab666@gmail.com> Date: Fri, 11 Sep 2020 17:44:55 +0200 Subject: [PATCH 1/6] adding the 10 exercises files to Week1-Tarek branch --- Week1/Homework/Js-exercises/Compare-arrays.js | 19 ++++++ ...00\272Userscomputer_nameAppDataRoamingnpm" | 0 .../Homework/Js-exercises/Error-debugging.js | 6 ++ .../Js-exercises/Log-an-array'animals.js | 21 ++++++ .../Js-exercises/Log-the-length-of-string.js | 13 ++++ Week1/Homework/Js-exercises/Log-the-number.js | 24 +++++++ .../Js-exercises/Log-the-remainder.js | 18 +++++ Week1/Homework/Js-exercises/Log-the-string.js | 24 +++++++ Week1/Homework/Js-exercises/Round-a-number.js | 19 ++++++ Week1/Homework/Js-exercises/Type-checker.js | 68 +++++++++++++++++++ Week1/Homework/Js-exercises/logHello.js | 22 ++++++ 11 files changed, 234 insertions(+) create mode 100644 Week1/Homework/Js-exercises/Compare-arrays.js create mode 100644 "Week1/Homework/Js-exercises/C\357\200\272Userscomputer_nameAppDataRoamingnpm" create mode 100644 Week1/Homework/Js-exercises/Error-debugging.js create mode 100644 Week1/Homework/Js-exercises/Log-an-array'animals.js create mode 100644 Week1/Homework/Js-exercises/Log-the-length-of-string.js create mode 100644 Week1/Homework/Js-exercises/Log-the-number.js create mode 100644 Week1/Homework/Js-exercises/Log-the-remainder.js create mode 100644 Week1/Homework/Js-exercises/Log-the-string.js create mode 100644 Week1/Homework/Js-exercises/Round-a-number.js create mode 100644 Week1/Homework/Js-exercises/Type-checker.js create mode 100644 Week1/Homework/Js-exercises/logHello.js diff --git a/Week1/Homework/Js-exercises/Compare-arrays.js b/Week1/Homework/Js-exercises/Compare-arrays.js new file mode 100644 index 000000000..1e632c989 --- /dev/null +++ b/Week1/Homework/Js-exercises/Compare-arrays.js @@ -0,0 +1,19 @@ +"use strict"; + +/*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 +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 +*/ + +let mySkills = ["HTML", "CSS", "JavaScript", "Java"]; +let food = ["Meat", "Fish", "fastfood", "Rice", "Sandwish", "Kebab", "Sushi"]; + +console.log( + `The length of mySkill is : ${mySkills.length} \n The length of food is : ${food.length}` +); + +if (mySkills.length === food.length) { + console.log("The length is the same!!"); +} else { + console.log("The length is not the same!!"); +} diff --git "a/Week1/Homework/Js-exercises/C\357\200\272Userscomputer_nameAppDataRoamingnpm" "b/Week1/Homework/Js-exercises/C\357\200\272Userscomputer_nameAppDataRoamingnpm" new file mode 100644 index 000000000..e69de29bb diff --git a/Week1/Homework/Js-exercises/Error-debugging.js b/Week1/Homework/Js-exercises/Error-debugging.js new file mode 100644 index 000000000..5928c360d --- /dev/null +++ b/Week1/Homework/Js-exercises/Error-debugging.js @@ -0,0 +1,6 @@ +'use strict' + +// console.log('I'm awesome'!; ======> it will give syntax error because of the usage of single qutation inside another single qutation !! + + +console.log("I'm awesome!"); \ No newline at end of file diff --git a/Week1/Homework/Js-exercises/Log-an-array'animals.js b/Week1/Homework/Js-exercises/Log-an-array'animals.js new file mode 100644 index 000000000..bfb1d642c --- /dev/null +++ b/Week1/Homework/Js-exercises/Log-an-array'animals.js @@ -0,0 +1,21 @@ +'use strict' + +/*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!*/ + +let animals = []; +console.log('its just an empty array since we have not added anything in it'); +console.log(animals); +let myFavAnimals = ['cat','lion','tiger']; +console.log(myFavAnimals); +myFavAnimals.push('Piglet'); +console.log(myFavAnimals); + + diff --git a/Week1/Homework/Js-exercises/Log-the-length-of-string.js b/Week1/Homework/Js-exercises/Log-the-length-of-string.js new file mode 100644 index 000000000..793a4ca0c --- /dev/null +++ b/Week1/Homework/Js-exercises/Log-the-length-of-string.js @@ -0,0 +1,13 @@ +"use strict"; + +/*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.*/ + +let mySentence = "Programming is so interesting!"; + +let length = mySentence.length; + +console.log(length); diff --git a/Week1/Homework/Js-exercises/Log-the-number.js b/Week1/Homework/Js-exercises/Log-the-number.js new file mode 100644 index 000000000..8314adafa --- /dev/null +++ b/Week1/Homework/Js-exercises/Log-the-number.js @@ -0,0 +1,24 @@ +"use strict"; + +/*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*/ + +let nubmerX; + +console.log("The value of X until now is undefined!!"); + +console.log(nubmerX); + +nubmerX = 30; + +console.log( + "we have assigned the value of 30 to the variable nubmerX , so now the value of this var is 30 !!" +); + +console.log(nubmerX); diff --git a/Week1/Homework/Js-exercises/Log-the-remainder.js b/Week1/Homework/Js-exercises/Log-the-remainder.js new file mode 100644 index 000000000..8420c65ea --- /dev/null +++ b/Week1/Homework/Js-exercises/Log-the-remainder.js @@ -0,0 +1,18 @@ +"use strict"; + +//If x equals 7, and the only other statement is x = x % 3, what would be the value of x after the calculation? + +/* in our example x = 7 , and % will search the nearest number to 7 that can be devide by 3 and the difference between 7 and the nearest number which is 6 +in this case will be (1) , so it will be the result */ + +//**************************************************************************************************************** + +//If y equals 21, and the only other statement is y = y % 4, what would be the value of y after the calculation? + +/* here the out put will be also (1) , cause 21 cant be divided by 4 but number 20 can be , so the difference between 21 and 20 is also (1)*/ + +//**************************************************************************************************************** + +//If z equals 13, and the only other statement is z = z % 2, what would be the value of z after the calculation? + +/* here the out put will be also (1) , cause 13 cant be divided by 2 but number 12 can be , so the difference between 21 and 20 is also (1)*/ diff --git a/Week1/Homework/Js-exercises/Log-the-string.js b/Week1/Homework/Js-exercises/Log-the-string.js new file mode 100644 index 000000000..ce84b3e9b --- /dev/null +++ b/Week1/Homework/Js-exercises/Log-the-string.js @@ -0,0 +1,24 @@ +"use strict"; + +/*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.*/ + +let myString = "Tarek Aljabr"; + +console.log("The value of the myString now is : Tarek Aljabr"); + +console.log(myString); + +myString = "Mr Aljabr"; + +console.log( + "myString is reassigned to a new value which is added in line 9, the new value of myString is : Mr Aljabr" +); + +console.log(myString); diff --git a/Week1/Homework/Js-exercises/Round-a-number.js b/Week1/Homework/Js-exercises/Round-a-number.js new file mode 100644 index 000000000..f552eadb3 --- /dev/null +++ b/Week1/Homework/Js-exercises/Round-a-number.js @@ -0,0 +1,19 @@ +"use strict"; + +/*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.*/ + +let z = 7.25; +console.log(z); + +let a = Math.round(z); +console.log(a); + +let highestValue = Math.max(z, a); +console.log(highestValue); diff --git a/Week1/Homework/Js-exercises/Type-checker.js b/Week1/Homework/Js-exercises/Type-checker.js new file mode 100644 index 000000000..32e049e94 --- /dev/null +++ b/Week1/Homework/Js-exercises/Type-checker.js @@ -0,0 +1,68 @@ +"use strict"; + +// creating the type checker function + +function typeChecker(x, y) { + if (typeof x === typeof y) { + console.log("Variables Have the same type of data"); + } else { + console.log("They are not the same"); + } +} + +// Testing the function + +let myString = "Tarek"; +let myAge = 30; +let myHobbies = ["playing guitar", "swimming"]; + +typeChecker(myString, myHobbies); + +// Variables + +let x = 9; +let y = 67; +let z = ["Tarek", false, 30]; +let i = { name: "Tarek" }; + +// check Data Type + +console.log(typeof x, typeof y, typeof z, typeof i); + +// Check if data type is the same + +if (typeof x === typeof y) { + console.log("Same type"); +} else { + console.log("Not the same"); +} + +if (typeof x === typeof z) { + console.log("Same type"); +} else { + console.log("Not the same"); +} + +if (typeof x === typeof i) { + console.log("Same type"); +} else { + console.log("Not the same"); +} + +if (typeof y === typeof z) { + console.log("Same type"); +} else { + console.log("Not the same"); +} + +if (typeof y === typeof i) { + console.log("Same type"); +} else { + console.log("Not the same"); +} + +if (typeof z === typeof i) { + console.log("Same type"); +} else { + console.log("Not the same"); +} diff --git a/Week1/Homework/Js-exercises/logHello.js b/Week1/Homework/Js-exercises/logHello.js new file mode 100644 index 000000000..58aad12cc --- /dev/null +++ b/Week1/Homework/Js-exercises/logHello.js @@ -0,0 +1,22 @@ +"use strict"; + +/*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*/ + +function greeting(str) { + console.log(str); +} + +greeting("Hello World"); // English +greeting("مرحبا بالجميع"); // Arabic +greeting("Bonjour le monde"); // French +greeting("Hej Verden"); // Danish +greeting("Hej världen"); // Swidich +greeting("Selam Dünya"); // Turkish +greeting("こんにちは世界"); // Japanese +greeting("ନମସ୍କାର ବିଶ୍ୱବାସି"); // Mondo +greeting("Witaj świecie"); // Polish +greeting("Ciao mondo"); // Italian~$ node FILENAME.js From dc37b7b6ab1b7120cf14b7e6a43cf08917d123c9 Mon Sep 17 00:00:00 2001 From: Tarek <2b3zab666@gmail.com> Date: Fri, 18 Sep 2020 18:11:11 +0200 Subject: [PATCH 2/6] added the json package for testing --- Week1/Homework/package-lock.json | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Week1/Homework/package-lock.json diff --git a/Week1/Homework/package-lock.json b/Week1/Homework/package-lock.json new file mode 100644 index 000000000..48e341a09 --- /dev/null +++ b/Week1/Homework/package-lock.json @@ -0,0 +1,3 @@ +{ + "lockfileVersion": 1 +} From f944e5289bf7a9d73ff3333f254aa7139f126162 Mon Sep 17 00:00:00 2001 From: Tarek <2b3zab666@gmail.com> Date: Sat, 19 Sep 2020 00:00:22 +0200 Subject: [PATCH 3/6] Added the 5 homework files and the Grade-calculato project into branch Week2-tarek --- .../homework/js-exercises/Grade-calculator.js | 56 +++++++++++++++++++ .../homework/js-exercises/Remove-the-comma.js | 25 +++++++++ Week2/homework/js-exercises/The-eve-odd.js | 14 +++++ .../homework/js-exercises/The-reading-list.js | 33 +++++++++++ .../homework/js-exercises/The-recipe-card.js | 19 +++++++ .../homework/js-exercises/Who-wants-drink.js | 27 +++++++++ 6 files changed, 174 insertions(+) create mode 100644 Week2/homework/js-exercises/Grade-calculator.js create mode 100644 Week2/homework/js-exercises/Remove-the-comma.js create mode 100644 Week2/homework/js-exercises/The-eve-odd.js create mode 100644 Week2/homework/js-exercises/The-reading-list.js create mode 100644 Week2/homework/js-exercises/The-recipe-card.js create mode 100644 Week2/homework/js-exercises/Who-wants-drink.js diff --git a/Week2/homework/js-exercises/Grade-calculator.js b/Week2/homework/js-exercises/Grade-calculator.js new file mode 100644 index 000000000..ef222f07d --- /dev/null +++ b/Week2/homework/js-exercises/Grade-calculator.js @@ -0,0 +1,56 @@ +"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*/ + +// creating the grade calculator funciton . + +function gradeCalculator(grade) { + // declaring the res variable which will contain the result of our calculator + + let res; + + // switch statment to give the res variable a value of [A or B or C or D or E or F] based on the score which is given as a parameter in our function + + switch (true) { + // here in Case 1 we are checken if the grade is equal or greater than 90 and at the same time is equal or less than 100 then our variable res will be 'A' + case grade >= 90 && grade <= 100: + res = "A"; + // break statment to stop the function here if the first case is matched + break; + // same logic but difference in ranges and value of res based on the grade + case grade >= 80 && grade <= 89: + res = "B"; + + break; + + case grade >= 70 && grade <= 79: + res = "C"; + + break; + + case grade >= 60 && grade <= 69: + res = "D"; + + break; + + case grade >= 50 && grade <= 59: + res = "E"; + + break; + + case grade >= 0 && grade <= 49: + res = "F"; + + break; + } + + console.log(`You got ${res} ${grade}%`); +} + +gradeCalculator(75); +gradeCalculator(95); +gradeCalculator(35); diff --git a/Week2/homework/js-exercises/Remove-the-comma.js b/Week2/homework/js-exercises/Remove-the-comma.js new file mode 100644 index 000000000..b53d0091b --- /dev/null +++ b/Week2/homework/js-exercises/Remove-the-comma.js @@ -0,0 +1,25 @@ +"use strict"; +// an exercise to train on spilt and join methods + +// 1- Add the variable to your file. + +let myString = "hello,this,is,a,difficult,to,read,sentence"; + +// 2- Log the length of myString.. + +console.log(myString.length); + +/* using the spilt command to have an array including the elements before and after the comma , then on the same line using the join method to convert the new + array into a string again replacing the , with spaces +*/ +const myNewString = myString.split(",").join(" "); + +console.log(myNewString); + +// Another sulotion using the replace() method . + +// here we are replacing the , with a space and we used the g flag to make it general so it will replace all the commas in the string not only the first one + +let myReplacedString = myString.replace(/,/g, " "); + +console.log(myReplacedString); diff --git a/Week2/homework/js-exercises/The-eve-odd.js b/Week2/homework/js-exercises/The-eve-odd.js new file mode 100644 index 000000000..7a606e14a --- /dev/null +++ b/Week2/homework/js-exercises/The-eve-odd.js @@ -0,0 +1,14 @@ +"use strict"; + +// 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! + +for (let i = 0; i <= 20; i++) { + if (i % 2 === 0) { + console.log(i, "is even!"); + } else { + console.log(i, "is odd!"); + } +} diff --git a/Week2/homework/js-exercises/The-reading-list.js b/Week2/homework/js-exercises/The-reading-list.js new file mode 100644 index 000000000..9cdb79885 --- /dev/null +++ b/Week2/homework/js-exercises/The-reading-list.js @@ -0,0 +1,33 @@ +"use strict"; + +/*1-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).*/ +//2-Loop through the array of books. +//3-For each book, log the book title and book author like so: "The Hobbit by J.R.R. Tolkien". +/*4-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*/ +//5-If you haven't read it log a string like You still need to read "The Lord of the Rings" + +let myBooks = [ + { + title: "The C Programming Language", + author: "Andy Hunt", + isReaded: false, + }, + { title: "Clean Code", author: "Robert C. Martin", isReaded: true }, + { + title: "The Mythical Man", + author: "Frederick P. Brooks Jr.", + isReaded: false, + }, +]; + +for (let book of myBooks) { + console.log(`${book.title} written By : ${book.author}`); + + if (book.isReaded === true) { + console.log(`You have already readed this book : ${book.title}`); + } else { + console.log(`You didn't read this book yet : ${book.title}`); + } +} diff --git a/Week2/homework/js-exercises/The-recipe-card.js b/Week2/homework/js-exercises/The-recipe-card.js new file mode 100644 index 000000000..3756d328c --- /dev/null +++ b/Week2/homework/js-exercises/The-recipe-card.js @@ -0,0 +1,19 @@ +"use strict"; + +// training on loops on objects + +//1- Declare a variable that holds an empty object literal (your meal recipe). + +let myMeal = {}; + +//2- Give the object 3 properties: a title (string), a servings (number) and an ingredients (array of strings) property. + +myMeal.title = "Chicken Crispy"; +myMeal.servings = 4; +myMeal.ingredients = ["Chicken wings", "Curry suase", "tow eggs"]; + +//3- Log each property out separately, using a loop (for, while or do/while) + +for (let prop in myMeal) { + console.log(`The property is ${prop} and it's value is ${myMeal[prop]}`); +} diff --git a/Week2/homework/js-exercises/Who-wants-drink.js b/Week2/homework/js-exercises/Who-wants-drink.js new file mode 100644 index 000000000..9acd8db06 --- /dev/null +++ b/Week2/homework/js-exercises/Who-wants-drink.js @@ -0,0 +1,27 @@ +"use strict"; +//1- Declare a variable that holds an empty array, called drinkTray. + +let drinkTray = []; + +const drinkTypes = ["cola", "lemonade", "water"]; + +/*2- 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. */ +//3- If there are already two instances of a drinkType then start with the next drink in the array. +//4- Your drinkTray should contain 2 cola, 2 lemonade and 1 water. +//5- 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!") + +for (let i = 0; i < 5; i++) { + // here the loop will iterate 5 times , so the index will be 1 , 2 , 3 , 4 , 5 >> but in drinkTypes array we have only three elemnts + // which means only the first three elements will be pushed into drinkTray array then the last tow will be undefined so to solve this + // we actually have to devide the index by 2, so the loop will be : + // iterate 1 >>> index is 0 / 2 = 0 after using floor method ==> index 0 is cola; + // iterate 2 >>> index is 1 / 2 = 0.5 after using floor method ==> index 0 is cola; + // iterate 3 >>> index is 2 / 2 = 1 ==> index 1 is lemonade; + // iterate 4 >>> index is 3 / 2 = 1.5 after using floor method ==> index 1 is lemonade; + // iterate 5 >>> index is 4 / 2 = 2 ==> index 2 is water; + + drinkTray.push(drinkTypes[Math.floor(i / 2)]); +} + +console.log(`Hey Guyz , I brought ${drinkTray}`); From b78c4b981f91f05b488c2961fe5f0d950414e9f2 Mon Sep 17 00:00:00 2001 From: Tarek <2b3zab666@gmail.com> Date: Mon, 28 Sep 2020 16:50:39 +0200 Subject: [PATCH 4/6] adding the 5 exercises file and the credit card validatior --- .../js-exercises/Credit-card-validator.js | 71 +++++++++++++++++++ Week3/homework/js-exercises/Dog-years.js | 20 ++++++ .../Shopping-at-the-supermarkt.js | 34 +++++++++ Week3/homework/js-exercises/Total-cost.js | 33 +++++++++ .../be-your-own-fortune-teller.js | 47 ++++++++++++ Week3/homework/js-exercises/giveCompliment.js | 33 +++++++++ 6 files changed, 238 insertions(+) create mode 100644 Week3/homework/js-exercises/Credit-card-validator.js create mode 100644 Week3/homework/js-exercises/Dog-years.js create mode 100644 Week3/homework/js-exercises/Shopping-at-the-supermarkt.js create mode 100644 Week3/homework/js-exercises/Total-cost.js create mode 100644 Week3/homework/js-exercises/be-your-own-fortune-teller.js create mode 100644 Week3/homework/js-exercises/giveCompliment.js diff --git a/Week3/homework/js-exercises/Credit-card-validator.js b/Week3/homework/js-exercises/Credit-card-validator.js new file mode 100644 index 000000000..cdf516cc6 --- /dev/null +++ b/Week3/homework/js-exercises/Credit-card-validator.js @@ -0,0 +1,71 @@ +"use strict"; + +/*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 +*/ + +function validateCreditNumber(number) { + + + // we convert the string into an array using spilt method so we can use the Array's method; + let splittedNumber = number.split(""); + // checking if the Input is 16 characters . + if (number.length !== 16) { + console.log( + `Invalid!! The input ${number} Should be 16 characters , Not ${number.length}` + ); + } + // checking if there is a letter in the input, here we should use regular experssion and match method ; + else if (number.match(/[A-Za-z]/)) { + console.log( + `Invalid!! The input ${number} Should Contain only numbers !!!` + ); + } + + // another way to make sure that the input includes only numbers and also not special characters like @,!,+,etc .... + // using every method to check if all elements in the array are numbers + else if (!splittedNumber.every((char) => /^[0-9]/.test(char))) { + console.log( + `Invalid!! The input ${number} Should Contain only numbers !!!` + ); + } + + //checking if At least two different numbers should be represented, to solve it we need to compare the 0 index of our array to all index in this array + // if its euqal then the input is only 1 number if its not then there is more than one number and it will be vaild + else if (splittedNumber.every((x) => x === splittedNumber[0])) { + console.log( + `Invalid!! The input ${number} Should have more than only one number !!!` + ); + } + + // The last number must be even + // to get the last number in the string we use number.length - 1 ==> we get the last index then we check if its odd or even + else if (number[number.length - 1] % 2 !== 0) { + console.log( + `Invalid!! The input ${number} Should end with an even number !!!` + ); + } + + //The sum of all the numbers must be greater than 16 + // to make it we need to use reduce method to sum all the elements of the array and get the result to check if it's greater than 16 or not + else if (splittedNumber.reduce((x, y) => parseInt(x) + parseInt(y)) < 16) { + console.log( + `Invalid!! The sum of input ${number} must be 16 or greater !!!` + ); + } + + else { `Success! The input ${number} is a valid credit card number!` } +} + +validateCreditNumber("0080010000111112"); +validateCreditNumber("0080010000111113"); +validateCreditNumber("00800100001111134"); +validateCreditNumber("00800100001111@4"); +validateCreditNumber("2222222222222222"); diff --git a/Week3/homework/js-exercises/Dog-years.js b/Week3/homework/js-exercises/Dog-years.js new file mode 100644 index 000000000..fb5b43cab --- /dev/null +++ b/Week3/homework/js-exercises/Dog-years.js @@ -0,0 +1,20 @@ +"use strict"; + +/* 1- Write a function named calculateDogAge. + 2- It takes 1 argument: your (fictional) puppy's age (number). + 3- Calculate your dog's age based on the conversion rate of 1 human year to 7 dog years. + 4- Return a string: "Your doggie is [CALCULATED_VALUE] years old in dog years!" + 5- Call the function three times with different sets of values. +*/ + +function calculateDogAge(age) { + let outPut = age * 7; + + // we have been asked to use return to return a string! + return `Your doggie is ${outPut} years old in dog years!`; +} + +// to check the out put we must print the whole function into the log, since we used return in the function not console.log +console.log(calculateDogAge(3)); +console.log(calculateDogAge(2)); +console.log(calculateDogAge(5)); diff --git a/Week3/homework/js-exercises/Shopping-at-the-supermarkt.js b/Week3/homework/js-exercises/Shopping-at-the-supermarkt.js new file mode 100644 index 000000000..6d6132dd0 --- /dev/null +++ b/Week3/homework/js-exercises/Shopping-at-the-supermarkt.js @@ -0,0 +1,34 @@ +"use strict"; + +/* 1- Create an array called shoppingCart that holds the following strings: "bananas" and "milk" + 2- Write a function named addToShoppingCart + 3- It takes 1 argument: a grocery item (string) + 4- Add grocery item to shoppingCart. If the amount of items is more than 3 remove the first one in the array + 5- Loops through the array in order to list out the items + 6- Return a string: "You bought [LIST_OF_GROCERY_ITEMS]!" + 7- Call the function 3 times, each time with a different string as the argument. +*/ + +const shoppingCart = ["bananas", "milk"]; + +function addToShoppingCart(item) { + shoppingCart.push(item); + // checking if the length of the array is greater than 3 then we will shift (delete) the first element in the array; + if (shoppingCart.length > 3) { + shoppingCart.shift(); + } + // outPutString is the variable which will hold the result + + let outPutString = "You bought "; + + for (let groceryItem of shoppingCart) { + //adding in each loop one value to the string + outPutString += " " + groceryItem; + } + + return outPutString; +} + +console.log(addToShoppingCart("water")); +console.log(addToShoppingCart("Chocolate")); +console.log(addToShoppingCart("watermelon")); diff --git a/Week3/homework/js-exercises/Total-cost.js b/Week3/homework/js-exercises/Total-cost.js new file mode 100644 index 000000000..5271ae4c2 --- /dev/null +++ b/Week3/homework/js-exercises/Total-cost.js @@ -0,0 +1,33 @@ +"use strict"; + +/* 1- 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) + 2- Write a function called calculateTotalPrice + 3- It takes 1 argument: an object that contains properties that only contain number values + 4- Loop through the object and add all the number values together + 5- Return a string: "Total: €[TOTAL_PRICE_ITEMS]" + 6- Call the function 1 time, giving it the object cartForParty as an argument +*/ + +const cartForParty = { + tomato: 2.35, + lemon: 4.49, + orange: 2, + beers: 1.25, + chips: 1, +}; + +function calculateTotalPrice(object) { + // first of all we declare the totalPrice as variable and typeof number so we dont get NaN in our result when want to sum the values; + let totalPrice = 0; + + // looping through the object to get the keys as itemPrice and the values of the keys as object[itemPrice]; + for (let itemPrice in object) { + // the total price will be 0 + each value of proprites in the object ; + + totalPrice += object[itemPrice]; + } + + return totalPrice; +} + +console.log(calculateTotalPrice(cartForParty)); diff --git a/Week3/homework/js-exercises/be-your-own-fortune-teller.js b/Week3/homework/js-exercises/be-your-own-fortune-teller.js new file mode 100644 index 000000000..7a6224b72 --- /dev/null +++ b/Week3/homework/js-exercises/be-your-own-fortune-teller.js @@ -0,0 +1,47 @@ +"use strict"; + +/* 1- 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 + 2- Write a function named tellFortune. + 3- It takes 4 arguments: number of children (number), partner's name (string), geographic location (string), job title (string). + 4- Randomly select values from the arrays. + 5- Return a string: "You will be a [JOB_TITLE] in [LOCATION], married to [PARTNER_NAME] with [NUMBER_KIDS] kids." + 6- Call the function 3 times, by passing the arrays as the argument. +*/ + +const numChilderen = [1, 2, 3, 4, 5]; +const partnerNames = ["Loubna", "Sara", "Mira", "Liza", "Marry"]; +const locations = ["Netherland", "Germany", "Us", "Dubai", "Spain"]; +const jobs = [ + "Taxi-driver", + "Teacher", + "Website developer", + "Engineer", + "Farmer", +]; + +function tellFortune(numChilderen, partnerNames, locations, jobs) { + // since we know that all arrays has the same length so we put it as a static value of 5; + let randomValue = Math.floor(Math.random() * 5); + // everytime we execute the function it will give us a random value of each array + let kidsNum = numChilderen[randomValue]; + let partner = partnerNames[randomValue]; + let location = locations[randomValue]; + let job = jobs[randomValue]; + //using console.log without using return so we don't have to log the funciton each time, only calling the funciton will give us the out put in the console + + // checking the number of the kids, if its more than 1 then it will be kids , if its only one then it will be kid. + + if (kidsNum > 1) { + console.log( + `You will be a ${job} in ${location}, married to ${partner} with ${kidsNum} kids.` + ); + } else { + console.log( + `You will be a ${job} in ${location}, married to ${partner} with Only ${kidsNum} kid.` + ); + } +} + +tellFortune(numChilderen, partnerNames, locations, jobs); +tellFortune(numChilderen, partnerNames, locations, jobs); +tellFortune(numChilderen, partnerNames, locations, jobs); diff --git a/Week3/homework/js-exercises/giveCompliment.js b/Week3/homework/js-exercises/giveCompliment.js new file mode 100644 index 000000000..2a9b09d9e --- /dev/null +++ b/Week3/homework/js-exercises/giveCompliment.js @@ -0,0 +1,33 @@ +"use strict"; + +/* 1- Write a function named giveCompliment + 2- It takes 1 argument: your name + 3- Inside the function define a variable that holds an array, compliments, with 10 strings. Each string should be a compliment, like "great", "awesome" + 4- Write code that randomly selects a compliment + 5- Return a string: "You are [COMPLIMENT], [YOUR_NAME]! + 6- Call the function three times, giving each function call the same argument: your name. +*/ + +function giveCompliment(name) { + const complimentsArray = [ + " very nice", + "Very helpful", + "really amazing", + "great!!", + "Very Smart", + "very good", + " wonderful", + "very impressive", + "handy", + "a beatiful person", + ]; +// creating a random number which will be the index so we can get a random number everytime, then we use it as an index of the array!! + const randomElement = Math.floor(Math.random() * complimentsArray.length); + + console.log(` You Are ${complimentsArray[randomElement]} ${name}`); +} + +giveCompliment("Tarek"); +giveCompliment("Tarek"); +giveCompliment("Tarek"); + From 67cd23e28c6ff39760e5d80410a518ed293ba2fa Mon Sep 17 00:00:00 2001 From: Tarek <2b3zab666@gmail.com> Date: Mon, 28 Sep 2020 16:59:34 +0200 Subject: [PATCH 5/6] adding exercise who wants a drink --- Week2/homework/js-exercises/Who-wants-drink.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Week2/homework/js-exercises/Who-wants-drink.js b/Week2/homework/js-exercises/Who-wants-drink.js index 9acd8db06..ccfb2183a 100644 --- a/Week2/homework/js-exercises/Who-wants-drink.js +++ b/Week2/homework/js-exercises/Who-wants-drink.js @@ -11,7 +11,7 @@ the same drink type, for example it can only hold 2 colas, 2 lemonades, 2 waters //4- Your drinkTray should contain 2 cola, 2 lemonade and 1 water. //5- 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!") -for (let i = 0; i < 5; i++) { +for (let i = 0; i < drinkTypes.length; i++) { // here the loop will iterate 5 times , so the index will be 1 , 2 , 3 , 4 , 5 >> but in drinkTypes array we have only three elemnts // which means only the first three elements will be pushed into drinkTray array then the last tow will be undefined so to solve this // we actually have to devide the index by 2, so the loop will be : @@ -21,7 +21,11 @@ for (let i = 0; i < 5; i++) { // iterate 4 >>> index is 3 / 2 = 1.5 after using floor method ==> index 1 is lemonade; // iterate 5 >>> index is 4 / 2 = 2 ==> index 2 is water; - drinkTray.push(drinkTypes[Math.floor(i / 2)]); + drinkTray.push(drinkTypes[i]) + + + //drinkTray.push(drinkTypes[Math.floor(i / 2)]); } -console.log(`Hey Guyz , I brought ${drinkTray}`); +//console.log(`Hey Guyz , I brought ${drinkTray}`); +console.log(drinkTray); \ No newline at end of file From 41d7355fcbd0126d4be9735255a2f4c4f0bfecd1 Mon Sep 17 00:00:00 2001 From: Tarek <2b3zab666@gmail.com> Date: Mon, 28 Sep 2020 17:02:40 +0200 Subject: [PATCH 6/6] added the last file --- Week1/Homework/Js-exercises/Compare-arrays.js | 1 + package-lock.json | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 package-lock.json diff --git a/Week1/Homework/Js-exercises/Compare-arrays.js b/Week1/Homework/Js-exercises/Compare-arrays.js index 1e632c989..e031e22ce 100644 --- a/Week1/Homework/Js-exercises/Compare-arrays.js +++ b/Week1/Homework/Js-exercises/Compare-arrays.js @@ -1,5 +1,6 @@ "use strict"; +require('foo'); /*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 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/package-lock.json b/package-lock.json new file mode 100644 index 000000000..48e341a09 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,3 @@ +{ + "lockfileVersion": 1 +}