From 3362cb16653ef88b8336fde7122d8179c01bc796 Mon Sep 17 00:00:00 2001 From: PrashantSalaveJosh Date: Fri, 18 Aug 2023 10:28:48 +0530 Subject: [PATCH 1/2] completed javascript assignment 6 --- Question1.js | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++ Question2.js | 35 ++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 Question1.js create mode 100644 Question2.js diff --git a/Question1.js b/Question1.js new file mode 100644 index 0000000..c619fa7 --- /dev/null +++ b/Question1.js @@ -0,0 +1,102 @@ +/* +Q1. Take the above array of objects. Accomplish the following tasks: +Write a function filterByName that accepts a string as a parameter and returns +an array with only those objects where the first_name field includes that +string. +Use Array.map to return an array of all the email fields. +Use Array.sort to return the array sorted in descending order by + date_of_birth. +Write a function getById that accepts a number as a parameter and returns + the object where the id is equal to that number. + */ + +const employeeDetails = [ + { + id: 1, + first_name: "Nicki", + email: "ncrozier0@squarespace.com", + date_of_birth: "2009/05/09", + }, + { + id: 2, + first_name: "Raychel", + email: "rmcgrady1@cpanel.net", + date_of_birth: "1996/11/05", + }, + { + id: 3, + first_name: "Demetris", + email: "dkilshall2@elpais.com", + date_of_birth: "2018/12/31", + }, + { + id: 4, + first_name: "Amata", + email: "abraiden3@canalblog.com", + date_of_birth: "2012/05/23", + }, + { + id: 5, + first_name: "Venita", + email: "vheap4@clickbank.net", + date_of_birth: "2020/10/04", + }, + { + id: 6, + first_name: "Fairfax", + email: "fcrichton5@merriam-webster.com", + date_of_birth: "2009/12/23", + }, + { + id: 7, + first_name: "Kathleen", + email: "kvasyukhnov6@devhub.com", + date_of_birth: "2010/12/20", + }, + { + id: 8, + first_name: "Sam", + email: "scorck7@sitemeter.com", + date_of_birth: "2020/08/30", + }, + { + id: 9, + first_name: "Virgilio", + email: "vferandez8@e-recht24.de", + date_of_birth: "2000/09/07", + }, + { + id: 10, + first_name: "Townie", + email: "tpetyt9@upenn.edu", + date_of_birth: "2018/09/01", + }, +]; + +const filterByName = (name) => { + let reqEmployees = employeeDetails.filter((emp) => emp.first_name === name); + return reqEmployees; +}; + +console.log(filterByName("Townie")); + +const getEmailIds = () => employeeDetails.map((emp) => emp.email); + +console.log(getEmailIds()); + +const sortByDate = () => { + return employeeDetails.sort((emp1, emp2) => { + const date1 = new Date(emp1.date_of_birth); + const date2 = new Date(emp2.date_of_birth); + + return date2 - date1; + }); +}; + +console.log(sortByDate()); + +const getById = (inputId) => { + return employeeDetails.filter((emp) => emp.id === inputId); +}; + +console.log(getById(1)); diff --git a/Question2.js b/Question2.js new file mode 100644 index 0000000..326d570 --- /dev/null +++ b/Question2.js @@ -0,0 +1,35 @@ +/* +2. What makes a method mutating or non mutating in Javascript? +Find out whether each of the following methods are mutating or non-mutating. +How can you verify this?: + +push +pop +filter +find +sort +map +*/ + +/* +Mutating methods- +Methods which make changes in same array, without creating new ones. +Eg:- +.push() — Adds a new item as the last item of the array. +.pop() — Removes the last item of the array. +.unshift() — Adds a new item as the first item of the array. +.shift() — Removes the first item of the array. +.reverse() — Reverses the order of the array. +.splice() — Removes/replaces item(s) in the array. +.sort() — Re-orders the items in the array based on their Unicode code points. +.copyWithin() — Copies one part of the array and puts it on another part of the same array. +.fill() — Changes some or all items in the array into the value being passed in. + +Non-Mutating- +Methods which returns new array as result. +Eg:- +.filter()-creates new array on the basis of given condition +.find()-returns the first index of the given element that satisfies the condition +.map()-returns the new array by applying the various operation given to it. + +*/ From 23e2eccc6083c177e17e05ad5c8197a9a8274895 Mon Sep 17 00:00:00 2001 From: PrashantSalaveJosh Date: Tue, 29 Aug 2023 11:00:14 +0530 Subject: [PATCH 2/2] changes in Question 1 --- Question1.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Question1.js b/Question1.js index c619fa7..9de7faf 100644 --- a/Question1.js +++ b/Question1.js @@ -74,11 +74,13 @@ const employeeDetails = [ ]; const filterByName = (name) => { - let reqEmployees = employeeDetails.filter((emp) => emp.first_name === name); + let reqEmployees = employeeDetails.filter((emp) => + emp.first_name.includes(name) + ); return reqEmployees; }; -console.log(filterByName("Townie")); +console.log(filterByName("Nic")); const getEmailIds = () => employeeDetails.map((emp) => emp.email);