-
-
Save codebubb/3221d1b878ea6ec0e1b513e21f17ef99 to your computer and use it in GitHub Desktop.
| const users = [ | |
| { id: '88f24bea-3825-4237-a0d1-efb6b92d37a4', firstName: 'Sam', lastName: 'Hughes' }, | |
| { id: '2a35032d-e02b-4508-b3b5-6393aff75a53', firstName: 'Terri', lastName: 'Bishop' }, | |
| { id: '7f053852-7440-4e44-838c-ddac24611050', firstName: 'Jar', lastName: 'Burke' }, | |
| { id: 'd456e3af-596a-4224-b1dc-dd990a34c9cf', firstName: 'Julio', lastName: 'Miller' }, | |
| { id: '58a1e37b-4b15-47c1-b95b-11fe016f7b64', firstName: 'Chester', lastName: 'Flores' }, | |
| { id: 'b4a306cb-8b95-4f85-b9f8-434dbe010985', firstName: 'Madison', lastName: 'Marshall' }, | |
| { id: '6ee904be-e3b0-41c9-b7a2-5a0233c38e4c', firstName: 'Ava', lastName: 'Pena' }, | |
| { id: '7f0ce45a-bdca-4067-968b-d908e79276ce', firstName: 'Gabriella', lastName: 'Steward' }, | |
| { id: '9e525c2d-6fcd-4d88-9ac4-a44eaf3a43e6', firstName: 'Charles', lastName: 'Campbell' }, | |
| { id: 'e789565f-fa5a-4d5e-8f6c-dd126cf995be', firstName: 'Madison', lastName: 'Lambert' }, | |
| ]; | |
| const comments = [ | |
| { userId: '88f24bea-3825-4237-a0d1-efb6b92d37a4', text: 'Great Job!' }, | |
| { userId: '7f053852-7440-4e44-838c-ddac24611050', text: 'Well done, I think I understand now!' }, | |
| { userId: 'e789565f-fa5a-4d5e-8f6c-dd126cf995be', text: 'How do you do that? π²' }, | |
| { userId: '7f053852-7440-4e44-838c-ddac24611050', text: 'OK great thanks' }, | |
| { userId: 'b4a306cb-8b95-4f85-b9f8-434dbe010985', text: 'Cool, thanks!' }, | |
| { userId: '9e525c2d-6fcd-4d88-9ac4-a44eaf3a43e6', text: 'Nice one π' }, | |
| { userId: '6ee904be-e3b0-41c9-b7a2-5a0233c38e4c', text: 'Got it.' }, | |
| { userId: '9e525c2d-6fcd-4d88-9ac4-a44eaf3a43e6', text: 'Thanks!' }, | |
| { userId: '58a1e37b-4b15-47c1-b95b-11fe016f7b64', text: 'Cool π' }, | |
| { userId: '6ee904be-e3b0-41c9-b7a2-5a0233c38e4c', text: 'Great stuff!' }, | |
| ]; | |
| // Exercises | |
| // 1) What is Madison Marshall's user id? | |
| // 2) Who wrote the first comment (assuming the first comment is in position 0 of the comments array) | |
| // 3) Which user commented 'OK great thanks'? | |
| // 4) Add the user's first and last name to each comment in the comments array | |
| // 5) Get a list of the users who haven't commented | |
// Exercise 2
const user = users.find(e => e.id == comments[0].userId);
// Exercise 3
const user = users.find(u => u.id == comments.find(c => c.text == 'OK great thanks').userId);
// Exercise 4
comments.forEach((c) => {
user = users.find( u => u.id == c.userId);
c.userFirstName = user.firstName;
c.userLastName = user.lastName;
})
// Exercise 5
const usersWithoutComments = users.find(u => !comments.some(c => c.userId == u.id));
// Exercises
// 1) What is Madison Marshall's user id?
const madisonMarshalId = users.find((user) => user.firstName === "Madison" && user.lastName === "Marshall").id;
console.log("Madison Marshall's user id --> ", madisonMarshalId);
// 2) Who wrote the first comment (assuming the first comment is in position 0 of the comments array)
const firstCommentWroteBy = users.find((user) => user.id === comments[0].userId);
console.log("Who wrote the first comment (assuming the first comment is in position 0 of the comments array) --> ", firstCommentWroteBy);
// 3) Which user commented 'OK great thanks'?
const findCommentUserId = comments.find((comment) => comment.text === "OK great thanks").userId;
const userBasedonCommentId = users.find((user) => user.id === findCommentUserId);
console.log("Which user commented 'OK great thanks' --> ", userBasedonCommentId);
// 4) Add the user's first and last name to each comment in the comments array
const findUserBasedonUSerID = (id) => users.find((user) => user.id === id);
const addFirstLastNameToComments = comments.map((comment) => {
let user = findUserBasedonUSerID(comment.userId);
return { ...comment, firstName: user.firstName, lastName: user.lastName };
});
console.log("Modified Commnts array with first and last name --> ", addFirstLastNameToComments);
// 5) Get a list of the users who haven't commented
const userCommentedOrNot = (userId) => comments.find((comment) => comment.userId === userId);
const usersWhoNotCommented = users.filter((user) => !userCommentedOrNot(user.id));
console.log("Users wo not commented -->", usersWhoNotCommented);
// 1) What is Madison Marshall's user id?
const userIdByName = (first, last) => users.find(user => user.firstName == first && user.lastName == last).id
console.log(userIdByName('Madison', 'Marshall'));
**// 2) Who wrote the first comment (assuming the first comment is in position 0 of the comments array)**
const whoWroteFirst = users.find(user => user.id === comments[0].userId);
console.log(whoWroteFirst);
// 3) Which user commented 'OK great thanks'?
const checkUserByComment = users.find(user => user.id === comments.find(comment => comment.text === 'OK great thanks').userId)
console.log(checkUserByComment);
// 4) Add the user's first and last name to each comment in the comments array
const addingFirstlastName = comments.map(comment => {
const {firstName, lastName} = users.find(user => comment.userId === user.id)
return {...comment, firstName, lastName};
})
console.table(addingFirstlastName);
// 5) Get a list of the users who haven't commented
const notCommented = users.filter(user => !comments.find(comment => comment.userId == user.id ))
console.log(notCommented);
const USerId = (firstName, lastName, arr = users) =>
arr.filter((el) => {
if (el.firstName === firstName && el.lastName === lastName) {
console.log("id--> ", el.id);
}
});
USerId("Madison", "Marshall", users);
const findCommenter = (commentId) =>
users.find((el) => el.id === commentId).firstName;
console.log(findCommenter("88f24bea-3825-4237-a0d1-efb6b92d37a4"));
3
const findCommentUser = (comment) => {
const commentId = comments.find((el) => el.text === comment).userId;
return users.find((el) => el.id === commentId).firstName;
};
console.log(findCommentUser("OK great thanks"));
const userNotCommented = () =>
users.filter(
(user) => !comments.find((comment) => comment.userId === user.id)
);
console.log(userNotCommented());
for the people who want to solve through for loop
***** Exercise 1 *******
for (let i = 0; i<users.length; i++){
if(users[i].firstName === 'Madison' && users[i].lastName === 'Marshall'){
console.log(users[i].id);
}
}
****** Exercise 2 *******
for(let i=0; i<users.length; i++){
if(comments[0].userId === users[i].id){
console.log(users[i]) ;
}
}
***** Exercise 3 ******
let commentId;
for (i = 0; i<users.length; i++){
for(j = 0; j < comments.length; j++){
if(comments[j].text === 'OK great thanks'){
commentId = comments[j].userId
}
} if(commentId === users[i].id){
console.log(users[i])
}
}
**** Exercise 4 ****
for ( let i = 0; i < users.length; i++){
for ( let j = 0; j < comments.length; j++){
if ( comments[j].userId === users[i].id ){
console.log({...comments[j], firstName: users[i].firstName, lastName: users[i].lastName})
}
}
}
If any one know the answer of the last exercise in for loop then kindly post
const users = [
{ id: '88f24bea-3825-4237-a0d1-efb6b92d37a4', firstName: 'Sam', lastName: 'Hughes' },
{ id: '2a35032d-e02b-4508-b3b5-6393aff75a53', firstName: 'Terri', lastName: 'Bishop' },
{ id: '7f053852-7440-4e44-838c-ddac24611050', firstName: 'Jar', lastName: 'Burke' },
{ id: 'd456e3af-596a-4224-b1dc-dd990a34c9cf', firstName: 'Julio', lastName: 'Miller' },
{ id: '58a1e37b-4b15-47c1-b95b-11fe016f7b64', firstName: 'Chester', lastName: 'Flores' },
{ id: 'b4a306cb-8b95-4f85-b9f8-434dbe010985', firstName: 'Madison', lastName: 'Marshall' },
{ id: '6ee904be-e3b0-41c9-b7a2-5a0233c38e4c', firstName: 'Ava', lastName: 'Pena' },
{ id: '7f0ce45a-bdca-4067-968b-d908e79276ce', firstName: 'Gabriella', lastName: 'Steward' },
{ id: '9e525c2d-6fcd-4d88-9ac4-a44eaf3a43e6', firstName: 'Charles', lastName: 'Campbell' },
{ id: 'e789565f-fa5a-4d5e-8f6c-dd126cf995be', firstName: 'Madison', lastName: 'Lambert' },
];
const comments = [
{ userId: '88f24bea-3825-4237-a0d1-efb6b92d37a4', text: 'Great Job!' },
{ userId: '7f053852-7440-4e44-838c-ddac24611050', text: 'Well done, I think I understand now!' },
{ userId: 'e789565f-fa5a-4d5e-8f6c-dd126cf995be', text: 'How do you do that? π²' },
{ userId: '7f053852-7440-4e44-838c-ddac24611050', text: 'OK great thanks' },
{ userId: 'b4a306cb-8b95-4f85-b9f8-434dbe010985', text: 'Cool, thanks!' },
{ userId: '9e525c2d-6fcd-4d88-9ac4-a44eaf3a43e6', text: 'Nice one π' },
{ userId: '6ee904be-e3b0-41c9-b7a2-5a0233c38e4c', text: 'Got it.' },
{ userId: '9e525c2d-6fcd-4d88-9ac4-a44eaf3a43e6', text: 'Thanks!' },
{ userId: '58a1e37b-4b15-47c1-b95b-11fe016f7b64', text: 'Cool π' },
{ userId: '6ee904be-e3b0-41c9-b7a2-5a0233c38e4c', text: 'Great stuff!' },
];
// Q4
comments.forEach((individualComments)=>{
let a = users.filter((individualUsers)=>{return individualUsers.id === individualComments.userId })
individualComments.firstName = (a[0]).firstName
individualComments.lastName = (a[0]).lastName
})
console.log(comments)
// Q5
let b = users.filter((individualUsers)=>{
if (comments.filter((individualComments)=>{return individualUsers.id === individualComments.userId}).length === 0) {
return individualUsers;
}
})
console.log(b);
// Exercise 1
const id = users.find( e => e.firstName == 'Madison' && e.lastName == 'Marshall').id