11import { 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
166181cleanConsole ( 7 , companies ) ;
@@ -177,7 +192,8 @@ console.log('---- EXAMPLE 7 part 5 --- ', f5(0)({name: 'Great Company'}));
177192console . log ( '---- EXAMPLE 7 part 6 --- ' , f6 ( 2 , 1 ) ) ;
178193console . log ( '---- EXAMPLE 7 part 7 --- ' , f7 ( 0 , 1 ) ( { firstName : 'Peter' } ) ) ;
179194console . 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