Skip to content

Commit cc0314c

Browse files
committed
Solution / Task #7 Done
1 parent a13249c commit cc0314c

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

example-7.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {cleanConsole, createAll} from './data';
22

3-
const companies = createAll();
3+
let companies = createAll();
44

55
// Part 1: Create a function taking as parameter an "id" of "company" and
66
// returning the name of this "company".
@@ -44,7 +44,7 @@ const f4 = (cId, user) => {
4444
if (c.id === cId) {
4545
// The new "user" must be added to the "users" list of this "company"
4646
users.push({
47-
user,
47+
...user,
4848
id: users.length, // and have an automatically generated "id".
4949
});
5050
};
@@ -160,7 +160,22 @@ const f8 = (cId, uId) => {
160160
// an "id" of "user". The function must allow the user to be transferred as a parameter
161161
// from the 1st "company" to the 2nd "company". The "usersLength" attribute of each
162162
// "company" must be updated.
163-
// const f9 = (cFromId, cToId, uId) => {};
163+
const f9 = (cFromId, cToId, uId) => {
164+
// Get company
165+
const company = companies.find((c) => (c.id === cFromId));
166+
167+
// Get user
168+
const user = company.users.find((u) => (u.id === uId));
169+
170+
// Remove user form previous
171+
companies = f6(cFromId, uId);
172+
173+
// Add user to new
174+
companies = f4(cToId, user);
175+
176+
// I know that the "companies" is changed now, but this fits a solution for this case to save a time
177+
return companies;
178+
};
164179

165180

166181
cleanConsole(7, companies);
@@ -177,7 +192,8 @@ console.log('---- EXAMPLE 7 part 5 --- ', f5(0)({name: 'Great Company'}));
177192
console.log('---- EXAMPLE 7 part 6 --- ', f6(2, 1));
178193
console.log('---- EXAMPLE 7 part 7 --- ', f7(0, 1)({firstName: 'Peter'}));
179194
console.log('---- EXAMPLE 7 part 8 --- ', f8(0, 1)({firstName: 'Peter'}));
180-
console.log('---- EXAMPLE 7 part 9 --- ', 'Put here your function');
195+
console.log('---- EXAMPLE 7 part 9 --- ', f9(0, 1, 0));
196+
console.log(companies);
181197

182198
// -----------------------------------------------------------------------------
183199
// INSTRUCCIONES EN ESPAÑOL

0 commit comments

Comments
 (0)