Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

completed javascript assignment 6 #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions Question1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
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: "[email protected]",
date_of_birth: "2009/05/09",
},
{
id: 2,
first_name: "Raychel",
email: "[email protected]",
date_of_birth: "1996/11/05",
},
{
id: 3,
first_name: "Demetris",
email: "[email protected]",
date_of_birth: "2018/12/31",
},
{
id: 4,
first_name: "Amata",
email: "[email protected]",
date_of_birth: "2012/05/23",
},
{
id: 5,
first_name: "Venita",
email: "[email protected]",
date_of_birth: "2020/10/04",
},
{
id: 6,
first_name: "Fairfax",
email: "[email protected]",
date_of_birth: "2009/12/23",
},
{
id: 7,
first_name: "Kathleen",
email: "[email protected]",
date_of_birth: "2010/12/20",
},
{
id: 8,
first_name: "Sam",
email: "[email protected]",
date_of_birth: "2020/08/30",
},
{
id: 9,
first_name: "Virgilio",
email: "[email protected]",
date_of_birth: "2000/09/07",
},
{
id: 10,
first_name: "Townie",
email: "[email protected]",
date_of_birth: "2018/09/01",
},
];

const filterByName = (name) => {
let reqEmployees = employeeDetails.filter((emp) =>
emp.first_name.includes(name)
);
return reqEmployees;
};

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function will return emp object if name exactly matches the emp.first_name, what we want is emp array where first_name field includes that string.

console.log(filterByName("Nic"));

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));
35 changes: 35 additions & 0 deletions Question2.js
Original file line number Diff line number Diff line change
@@ -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.

*/