From 65b225ae54ddc35f557e5c3a9dae418b6b798301 Mon Sep 17 00:00:00 2001 From: ozcansamed Date: Sun, 22 Sep 2019 15:19:07 +0200 Subject: [PATCH] Finished Homework week2 SAMED OZCAN --- .../js-exercises/PROJECT: Grade calculator.js | 26 ++++++++++++++ .../homework/js-exercises/ex-1-removeComma.js | 26 ++++++++++++++ .../js-exercises/ex-2-evenOddReporter.js | 16 +++++++++ .../homework/js-exercises/ex-3-recipeCard.js | 26 ++++++++++++++ .../homework/js-exercises/ex-4-readingList.js | 34 +++++++++++++++++++ .../js-exercises/ex-5-whoWantsDrink.js | 16 +++++++++ 6 files changed, 144 insertions(+) create mode 100644 Week2/homework/js-exercises/PROJECT: Grade calculator.js create mode 100644 Week2/homework/js-exercises/ex-1-removeComma.js create mode 100644 Week2/homework/js-exercises/ex-2-evenOddReporter.js create mode 100644 Week2/homework/js-exercises/ex-3-recipeCard.js create mode 100644 Week2/homework/js-exercises/ex-4-readingList.js create mode 100644 Week2/homework/js-exercises/ex-5-whoWantsDrink.js diff --git a/Week2/homework/js-exercises/PROJECT: Grade calculator.js b/Week2/homework/js-exercises/PROJECT: Grade calculator.js new file mode 100644 index 000000000..b0f1582c0 --- /dev/null +++ b/Week2/homework/js-exercises/PROJECT: Grade calculator.js @@ -0,0 +1,26 @@ +/* We made a function which takes score, evaluates between 0-100, assigns a grade letter +according two American grade system. */ + +function calculateGrade(score) { + let grade = ''; + + if (score >= 90 && score <= 100) { + grade = 'A'; + } else if (score >= 80 && score < 90) { + grade = 'B'; + } else if (score >= 70 && score < 80) { + grade = 'C'; + } else if (score >= 60 && score < 70) { + grade = 'D'; + } else if (score >= 50 && score < 60) { + grade = 'E'; + } else if (score >= 0 && score < 50) { + grade = 'F'; + } else { + grade = 'n/a'; //not available + } + + return `You got a ${grade} (${score}%)!`; +} + +console.log(calculateGrade(100)); diff --git a/Week2/homework/js-exercises/ex-1-removeComma.js b/Week2/homework/js-exercises/ex-1-removeComma.js new file mode 100644 index 000000000..a5783bce0 --- /dev/null +++ b/Week2/homework/js-exercises/ex-1-removeComma.js @@ -0,0 +1,26 @@ +'use strict'; +console.log('EXERCISE 1 - Remove The Comma'); + +/* EXERCISE-1 => +In this exercise we want to remove all the commas and replace them with spaces. +So I used google for my searching and I found the answer in stackoverflow.com +https://stackoverflow.com/questions/39345634/how-do-i-replace-all-spaces-commas-and-periods-in-a-variable-using-javascript/39346082 +To achieve this, we should use regular expressions(regex). +It has 2 parameters, first one is what we want to change, +and the second parameter is the replacing ones. +In the end our sentence is "hello this is a difficult to read sentence"*/ + +let myString = 'hello,this,is,a,difficult,to,read,sentence'; +console.log('The length of my string is ' + myString.length); +myString = myString.replace(/[","]/g, ' '); +console.log(myString); + +console.log('------------'); +/* Or we can do this with using split method and join method */ + +let myString2 = 'hello,this,is,a,difficult,to,read,sentence'; +console.log('The length of my string is ' + myString.length); +let splittedString = myString.split(','); +console.log(splittedString); +myString = splittedString.join(' '); +console.log(myString); diff --git a/Week2/homework/js-exercises/ex-2-evenOddReporter.js b/Week2/homework/js-exercises/ex-2-evenOddReporter.js new file mode 100644 index 000000000..fc56cfdc5 --- /dev/null +++ b/Week2/homework/js-exercises/ex-2-evenOddReporter.js @@ -0,0 +1,16 @@ +'use strict'; +console.log('EXERCISE 2 - The even/odd reporter'); + +/* EXERCISE-2 => +First we made a for loop which iterates 0 to 20. +Then we made an if + else statement to check if our number is odd or even. +To do this we used remainder/modulo operator. +finally we console.log our numbers. ta da...*/ + +for (let i = 0; i <= 20; i++) { + if (i % 2 === 1) { + console.log('The number ' + i + ' is odd!'); + } else { + console.log(`The number ${i} is even!`); + } +} diff --git a/Week2/homework/js-exercises/ex-3-recipeCard.js b/Week2/homework/js-exercises/ex-3-recipeCard.js new file mode 100644 index 000000000..0926a6207 --- /dev/null +++ b/Week2/homework/js-exercises/ex-3-recipeCard.js @@ -0,0 +1,26 @@ +'use strict'; +console.log('EXERCISE 3 - The Recipe Card'); + +/* EXERCISE-3 => +First of all we made an object with 3 properties. +Then we logged out the properties using loop. */ + +let myMealRecipe = { + nameOfMeal: 'meat saute', + serves: 4, + ingredients: ['1 kg meat', '7 tomatoes', '11 pepper', 'salt'], +}; + +console.log(`Meal name: ${myMealRecipe.nameOfMeal}`); // 'Meal name: ' + myMealRecipe.nameOfMeal +console.log(`Serves: ${myMealRecipe.serves}`); +console.log('Ingredients:'); +for (let i = 0; i < myMealRecipe.ingredients.length; ++i) { + console.log(myMealRecipe.ingredients[i]); +} + +/* Or we can do this with for..of..loop + +for (let ingredient of myMealRecipe.ingredients) { + console.log(ingredient); +} +*/ diff --git a/Week2/homework/js-exercises/ex-4-readingList.js b/Week2/homework/js-exercises/ex-4-readingList.js new file mode 100644 index 000000000..e38280261 --- /dev/null +++ b/Week2/homework/js-exercises/ex-4-readingList.js @@ -0,0 +1,34 @@ +'use strict'; +console.log('EXERCISE 4 - The reading list'); + +/* EXERCISE-4 => */ + +/* First we creat our objects, then using for loop and if +we can log our reading list. */ + +let myBookList = [ + { + title: 'The Count of Monte Cristo', + author: 'Alexander Dumas', + alreadyRead: true, + }, + { + title: 'Les Misarebles', + author: 'Victor Hugo', + alreadyRead: true, + }, + { + title: 'The Hobbit', + author: 'J.R.R. Tolkien', + alreadyRead: false, + }, +]; + +for (let i = 0; i < myBookList.length; ++i) { + console.log(myBookList[i].title + ' by ' + myBookList[i].author); + if (myBookList[i].alreadyRead) { + console.log(`You already read "${myBookList[i].title}"`); + } else { + console.log(`You still need to read "${myBookList[i].title}"`); + } +} diff --git a/Week2/homework/js-exercises/ex-5-whoWantsDrink.js b/Week2/homework/js-exercises/ex-5-whoWantsDrink.js new file mode 100644 index 000000000..c1cd777a9 --- /dev/null +++ b/Week2/homework/js-exercises/ex-5-whoWantsDrink.js @@ -0,0 +1,16 @@ +'use strict'; +console.log('EXERCISE 5 - Who wants a drink?'); + +/* EXERCISE-5 => */ + +/* We declared our empty array, then with for loop +we pushed our drinkTypes. Finally with back tick we logged our sentence. */ + +let drinkTray = []; +const drinkTypes = ['cola', 'lemonade', 'water']; + +for (let i = 0; i < 5; ++i) { + drinkTray.push(drinkTypes[i % 3]); +} + +console.log('Hey guys, I brought a ' + drinkTray.join(', ') + '!');