-
Notifications
You must be signed in to change notification settings - Fork 0
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
PrashantSalaveJosh
wants to merge
2
commits into
main
Choose a base branch
from
assignment/ps/js06
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
|
||
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
*/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.