From 1946f19132e96613f0720b58c12251b22e64190a Mon Sep 17 00:00:00 2001 From: musayuksel <79163385+musayuksel@users.noreply.github.com> Date: Sun, 18 Jul 2021 21:59:50 +0100 Subject: [PATCH] Add all mandatory homeworks --- mandatory/1-writers.js | 12 ++-- mandatory/10-cheap-diner.js | 22 ++++++ mandatory/11-choose-your-own-adventure.js | 22 +++++- mandatory/2-eligible-students.js | 5 +- mandatory/3-journey-planner.js | 11 ++- mandatory/4-water-bottle.js | 11 ++- mandatory/5-groceries.js | 32 +++++++-- mandatory/6-people-I-know.js | 35 +++++++--- mandatory/7-recipes.js | 39 ++++++++++- mandatory/8-reading-list.js | 82 ++++++++++++++++++++--- mandatory/9-budgets.js | 3 + 11 files changed, 242 insertions(+), 32 deletions(-) diff --git a/mandatory/1-writers.js b/mandatory/1-writers.js index f815c156..1daf7561 100644 --- a/mandatory/1-writers.js +++ b/mandatory/1-writers.js @@ -67,8 +67,7 @@ Exercise 1: "Hi, my name is {firstName} {lastName}. I am {age} years old, and work as a {occupation}." */ function logAllWriters() { - // write your code to log all writers here -}; + writers.forEach(writer => console.log(`Hi, my name is ${writer.firstName} ${writer.lastName}. I am ${writer.age} years old, and work as a ${writer.occupation}.`))}; /* Exercise 2: @@ -80,7 +79,10 @@ Exercise 2: */ function logDeadWritersInTheirForties() { - // write your code here + // CHOOSE WRITERS OVER 40 AND DIED + const writer40sAndDead = writers.filter(writer => writer.age >=40 && writer.age <=49 && !writer.alive ); + writer40sAndDead.forEach(elm => console.log(`Writer ${elm.firstName} ${elm.lastName} died at ${elm.age} years old.`)); + } /* @@ -92,7 +94,9 @@ Exercise 3: */ function logAliveWritersInTheirForties() { - // write your code here + // // CHOOSE WRITERS OVER 40 AND ALIVE + const writers40sAlive = writers.filter(writer => writer.age >=40 && writer.age <=49 && writer.alive ); + writers40sAlive.forEach(writer => console.log(`Hi, my name is ${writer.firstName} ${writer.lastName}. I am ${writer.age} years old.`)); } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/mandatory/10-cheap-diner.js b/mandatory/10-cheap-diner.js index 302dfbb9..b785166b 100644 --- a/mandatory/10-cheap-diner.js +++ b/mandatory/10-cheap-diner.js @@ -30,6 +30,28 @@ Should give the answer "Nothing :(" **/ function chooseMeal(mealArray) { + // IF ARRAY IS EMPTY OR IF IT HAS 1 MENU, FIND IT AND RETURN + if(mealArray.length === 0){ + return "Nothing :("; + } else if(mealArray.length === 1){ + return mealArray[0].name; + }else{ + let cheapestMenu= mealArray[0]; + let secondCheapestMenu= mealArray[1]; + // console.log(cheapestMenu.price,'<<<<<<<<<<<<<<<<<<<<<'); + // LOOK THROUGH ALL ARRAY, IF CURRENT MENU 'menu' PRICE CHEAPEST => + // CHANGE CHEAPEST MENU , ALSO OLD CHEAPEST WILL BECOME SECONDCHEAPEST + mealArray.forEach(menu => { + if(menu.price < cheapestMenu.price) { + secondCheapestMenu = cheapestMenu; + cheapestMenu = menu; + // IF CURRENT MENU PRICE BETWEEN CHEAPEST AND SECONDCHEAPEST => CHANGE SECONDCHEAPEST + } else if(menu.price > cheapestMenu.price && menu.price < secondCheapestMenu.price){ + secondCheapestMenu =menu; + } + }); + return secondCheapestMenu.name; + } } /* ======= TESTS - DO MODIFY (!!!) ===== diff --git a/mandatory/11-choose-your-own-adventure.js b/mandatory/11-choose-your-own-adventure.js index 1086bc0d..49448ecf 100644 --- a/mandatory/11-choose-your-own-adventure.js +++ b/mandatory/11-choose-your-own-adventure.js @@ -40,6 +40,11 @@ let game = { currentRoom: null, start: function (roomName) { + if(roomName === "hall" || roomName === "classroom" ||roomName === "library"){ + this.currentRoom = rooms[roomName]; + } else {console.log("YOU ENTER WRONG NAME YOU WILL GO TO THE HALL"); + this.currentRoom = rooms.hall + }; // This function is called with the name of the room that the player wants // to start in. // Finish the function so that the currentRoom property is set to the room @@ -48,7 +53,22 @@ let game = { // Hint: the only valid rooms are "hall", "classroom" and "library". }, - move: function (direction) { + move: function (dirc) { + //CHECK DIRETION, IT HAS TO BE RIGHT DIRECTION 'north/east/south/west' + if(dirc === 'north' || dirc === 'east' || dirc === 'south' || dirc === 'west' ){ + //FIND DIRECTION FUNCTION IN YOUR CURRENT ROOM. IT WILL RETURN FUNCTION + // ASSIGNT IT TO 'directionFunction + let directionFunction = this.currentRoom[dirc]; + //CALL THAT FUNCTION => IT WILL RETURN NULL OR NEW ROOM OBJECT, IF ITS A ROOM => ASSIGN IT TO CURRENT ROOM + // IF IT IS NULL, STAY ROOM AND GIVE MESSAGE + if(directionFunction()===null){ + console.log(`****YOU CAN NOT GO THAT WAY PLEASE TRY ANOTHER WAY***`); + }else{ + this.currentRoom = directionFunction(); + } + } else { + console.log(`YOU ENTER WRONG DIRECTION NAME, PLEASE CHECK AND TRY AGAIN`) + } // This function is called with the direction that the player wants to move. // Finish the function so that the currentRoom property is updated with new // room in the direction that the player wants to move in. diff --git a/mandatory/2-eligible-students.js b/mandatory/2-eligible-students.js index cb472063..8517b881 100644 --- a/mandatory/2-eligible-students.js +++ b/mandatory/2-eligible-students.js @@ -20,7 +20,10 @@ */ function eligibleStudents(attendances) { - + //filter objects over 8 + const over8Objects= attendances.filter(student => student.attendance >=8); + // take names + return over8Objects.map(student => student.name); } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/mandatory/3-journey-planner.js b/mandatory/3-journey-planner.js index 2a96b8a2..53185a9b 100644 --- a/mandatory/3-journey-planner.js +++ b/mandatory/3-journey-planner.js @@ -27,7 +27,16 @@ */ function journeyPlanner(locations, transportMode) { - + const avaibleLocations = []; + // check all locations + for(const key in locations){ + // if its avaible for transporMode add to the array (just names) + if(locations[key].includes(transportMode)){ + avaibleLocations.push(key); + } + } + //return array + return avaibleLocations; } /* ======= TESTS - DO NOT MODIFY ===== diff --git a/mandatory/4-water-bottle.js b/mandatory/4-water-bottle.js index 0301c25d..f7a197ca 100644 --- a/mandatory/4-water-bottle.js +++ b/mandatory/4-water-bottle.js @@ -20,22 +20,31 @@ You have to implement the missing features according to the specification. */ // Here is your starting point: -let bottle = { +const bottle = { volume: 0, fillUp: function () { // calling this function should completely fill your bottle (volume = 100); + this.volume =100; }, pour: function () { // calling this function should increase your bottle volume by 10 units; + this.volume +=10; + //if bottle is full, update volume = 100 + if(this.volume > 100) { this.volume =100;} }, drink: function () { // calling this function should decrease your bottle volume by 10 units; + this.volume -= 10; + //if bottle is empty, update volume = 0 + if(this.volume < 0) { this.volume = 0;} }, isFull: function () { // this function should return true if your bottle is full; + return this.volume === 100; }, isEmpty: function () { // this function should return true if your bottle is empty; + return this.volume === 0; }, }; diff --git a/mandatory/5-groceries.js b/mandatory/5-groceries.js index 2855fdcb..5a1e8936 100644 --- a/mandatory/5-groceries.js +++ b/mandatory/5-groceries.js @@ -12,7 +12,7 @@ Complete the exercises below. */ // Here is your -let weeklyMealPlan = { +const weeklyMealPlan = { monday: ["Cheese", "Eggs", "Tomato", "Paprika", "Leek"], tuesday: ["Wrap", "Tuna", "Canned beans", "Cheese", "Carrot", "Aubergine"], wednesday: ["Orange Juice", "Apple", "Ananas", "Black tea"], @@ -28,15 +28,33 @@ Exercise 1: The weeklyGroceriesToBuy array shouldn't contain any repeating items. */ // Gather all week item names into this array -let weeklyGroceriesToBuy = []; +const weeklyGroceriesToBuy = []; +// look through all days, +for(const day in weeklyMealPlan){ + //take all items one-by-one from daily array + weeklyMealPlan[day].forEach(items => { + // if it is NOT inside weeklyGroceriesToBuy, add it + if(!weeklyGroceriesToBuy.includes(items)){ weeklyGroceriesToBuy.push(items);} + }); +} /* Exercise 2: Loop through your list again, but now only collect the weekend items into the weekendGroceriesToBuy array. */ // Gather weekend item names into this array -let weekendGroceriesToBuy = []; - +const weekendGroceriesToBuy = []; +// look through all days, +for(const day in weeklyMealPlan){ + //if days are saturday or sunday + if(day === 'saturday' || day === 'sunday') { + //take all items one-by-one from daily array + weeklyMealPlan[day].forEach(items => { + // if it is NOT inside weeklyGroceriesToBuy, add it + if(!weekendGroceriesToBuy.includes(items)){ weekendGroceriesToBuy.push(items);} + }); + } +} /* Exercise 3: Loop through your weekly meal plan: @@ -44,7 +62,7 @@ Exercise 3: - and update the corresponding properties of numberOfItemsPerWeek object. */ // Gather daily item counts into this object -let numberOfItemsPerWeek = { +const numberOfItemsPerWeek = { monday: 0, tuesday: 0, wednesday: 0, @@ -53,6 +71,10 @@ let numberOfItemsPerWeek = { saturday: 0, sunday: 0, }; +for(const day in weeklyMealPlan){ + numberOfItemsPerWeek[day] = weeklyMealPlan[day].length; + // console.log(day,'<<<<< elem.age >=35);; /* 3) Find the email address @@ -390,8 +390,10 @@ let thirtyFiveOrOlder = []; Next, I want you to find all of my friends who work for "POWERNET" and then store their emails in the array below */ - -let powerNetEmails = []; +//FILTER FRIENDS OBJECTS WHO HAS POWERNET MAIL +const friendsWithPowernet =friends.filter(elem => elem.email.includes('powernet')); +// TAKE THEIR EMAIL ADDRESSES +const powerNetEmails = friendsWithPowernet.map(elem => elem.email); /* @@ -404,8 +406,16 @@ You can see who people's colleagues are by seeing the "colleagues" array in each This time, I only want the full names (" ") of my friends who are colleagues of hers. */ - -let friendsWhoAreColleaguesOfStacie = []; +// LOOK THROUGH ALL FRIENDS ARRAY (MAIN) +const friendsObjectWhoColleaguesOfStacie= friends.filter(friend => { + //FOR EVERY FRIEND, LOOK 'COLLEAGUES' ARRAY AND IF THERE IS A NAME "Stacie Villarreal"(at least one) INSIDE IT = FILTER THEM + return friend.colleagues.some(obj => obj.name === "Stacie Villarreal" ); + +}) +// TAKE THEIR NAMES AND LAST NAMES +const friendsWhoAreColleaguesOfStacie = friendsObjectWhoColleaguesOfStacie.map(friend => `${friend.name.first} ${friend.name.last}`); +//ORIGINAL ARRAY 'friendsWhoAreColleaguesOfStacie' TRUE BAT REVERSE, CHANGE IT +friendsWhoAreColleaguesOfStacie.reverse(); /* 5) Find "Multi-tasking" colleagues @@ -417,8 +427,17 @@ You can tell if they are good at "Multi-tasking" because they will have it liste This time, I only want the full names of the people who can multitask */ - -let colleaguesWhoCanMultitask = []; +// LOOK THROUGH ALL FRIENDS ARRAY (MAIN) +const colleaguesWhoCanMultitask =[]; +friends.forEach(friend =>{ + //FOR EVERY FRIEND, LOOK 'COLLEAGUES ' ARRAY + friend.colleagues.forEach(colleague => { + //IF 'SKILL' ARRAY INCLUDES(MULTUTASK) INSIDE IT = TAKE NAMES INTO ARRAY + if(colleague.skills.includes("Multi-tasking")){ + colleaguesWhoCanMultitask.push(`${colleague.name}`) + } + }) +}); /* ======= TESTS - DO NOT MODIFY ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 6-people-I-know.js` diff --git a/mandatory/7-recipes.js b/mandatory/7-recipes.js index da790642..00f681f8 100644 --- a/mandatory/7-recipes.js +++ b/mandatory/7-recipes.js @@ -24,5 +24,40 @@ You should write and log at least 5 recipes **/ -let recipes = {}; - +const recipes = { + coffee : { + names:'Filter Coffee', + service : 2, + ingredients :['ground coffee', 'french press', 'hot water'] + }, + pasta : { + names:'Spaghetti Bolognese', + service : 2, + ingredients :['pasta', 'bolognese souce', 'hot water','salt','yogurt'] + }, + cake : { + names:'Cocoa Cake', + service : 4, + ingredients :['flour', 'sugar', 'milk', 'egg', 'oil', 'cocoa'] + }, + mole : { + names:'Mole', + service : 2, + ingredients :['cinnamon', 'cumin', 'cocoa'] + }, + fish : { + names:'Fish', + service : 3, + ingredients :['fish', 'onion', 'bay leaf', 'salt', 'oil', 'garlic'] + } +}; + +for(const recipe in recipes){ + console.log(`${recipes[recipe].names}`); + console.log(`Serves: ${recipes[recipe].service}`); + console.log('Ingredients'); + recipes[recipe].ingredients.forEach(element => { + console.log(element); + }); + console.log('**************************'); +} \ No newline at end of file diff --git a/mandatory/8-reading-list.js b/mandatory/8-reading-list.js index 6d22df00..036ce793 100644 --- a/mandatory/8-reading-list.js +++ b/mandatory/8-reading-list.js @@ -24,11 +24,44 @@ without using any variables or any logic like loops, template strings or if stat */ -const books = []; +const books = [ + { + title : 'Doctor Who', + author :'Terrance Dicks', + isAlreadyRead : false + }, + { + title : 'Stargazing for Beginners', + author :'Jenny McLachlan', + isAlreadyRead : true + }, + { + title : 'Eagle Strike', + author :'Anthony Horowitz', + isAlreadyRead : true + }, + { + title : 'Unleash Hell', + author :'War Picture Library', + isAlreadyRead : false + }, + { + title : 'Diary of a Wimpy Kid Rodrick Rules', + author :'Jeff Kinney', + isAlreadyRead : true + }, + { + title : 'Too Good to Be True', + author :'Ann Cleeves', + isAlreadyRead : true + } +]; // exercise 1 -function logBooks() { +function logBooks(books) { + books.forEach(eachBook => console.log(`${eachBook.title} by ${eachBook.author}`)); } +logBooks(books); /* @@ -60,7 +93,18 @@ As an example for this exercise, you might do the following steps - All tests should turn green!! **/ - +function logBooksAlreadyRead(books){ + // LOOK THROUGH ALL OBJECT ARR + books.forEach(eachBook =>{ + // IF ALREADY READ / NOT = WRITE CORRECT FORMAT + if(eachBook.isAlreadyRead){ + console.log(`You've already read ${eachBook.title} by ${eachBook.author}`); + } else { + console.log(`You still need to read ${eachBook.title} by ${eachBook.author}`); + } + }); +} +logBooksAlreadyRead(books); /* ======= TESTS - DO MODIFY (!!!) ===== - To run the tests for this exercise, run `npm test -- --testPathPattern 8-reading-list.js` - To run all exercises/tests in the mandatory folder, run `npm test` @@ -69,11 +113,22 @@ As an example for this exercise, you might do the following steps test("books are logged", function() { expectLogBooksToLog([ - "The Hobbit by J.R.R. Tolkien", - "The Map of Salt and Stars by Jennifer Zeynab Joukhadar", - "Dietland by Sarai Walker", - "A Place for Us by Fatima Farheen Mirza", - "The House of Impossible Beauties by Joseph Cassara" + "Doctor Who by Terrance Dicks", + "Stargazing for Beginners by Jenny McLachlan", + "Eagle Strike by Anthony Horowitz", + "Unleash Hell by War Picture Library", + "Diary of a Wimpy Kid Rodrick Rules by Jeff Kinney", + "Too Good to Be True by Ann Cleeves" + ]); +}); +test("books are logged", function () { + expectaToLog([ + "You still need to read Doctor Who by Terrance Dicks", + "You've already read Stargazing for Beginners by Jenny McLachlan", + "You've already read Eagle Strike by Anthony Horowitz", + "You still need to read Unleash Hell by War Picture Library", + "You've already read Diary of a Wimpy Kid Rodrick Rules by Jeff Kinney", + "You've already read Too Good to Be True by Ann Cleeves" ]); }); @@ -88,7 +143,16 @@ test("books are logged", function() { */ function expectLogBooksToLog(expectedValues) { const consoleLogSpy = jest.spyOn(console, 'log'); - logBooks(); + logBooks(books); + expect(consoleLogSpy).toBeCalledTimes(expectedValues.length); + expectedValues.forEach((value, i) => { + expect(consoleLogSpy).nthCalledWith(i+1, value); + }); + consoleLogSpy.mockRestore(); +}; +function expectaToLog(expectedValues) { + const consoleLogSpy = jest.spyOn(console, 'log'); + logBooksAlreadyRead(books); expect(consoleLogSpy).toBeCalledTimes(expectedValues.length); expectedValues.forEach((value, i) => { expect(consoleLogSpy).nthCalledWith(i+1, value); diff --git a/mandatory/9-budgets.js b/mandatory/9-budgets.js index 2c05a4b5..f6d449a4 100644 --- a/mandatory/9-budgets.js +++ b/mandatory/9-budgets.js @@ -17,6 +17,9 @@ Should give return the answer of 62600. **/ function getBudgets(peopleArray) { + let totalBudget=0; + peopleArray.forEach(elem => totalBudget += elem.budget); + return totalBudget; } /* ======= TESTS - DO MODIFY (!!!) =====