@@ -204,4 +204,69 @@ console.log(gameEvents);
204204console . log ( `An event happened, on average, every ${ 90 / gameEvents . size } minutes` ) ;
205205
206206for ( let [ key , value ] of gameEvents )
207- console . log ( `[${ key <= 45 ? 'FIRST' : 'SECOND' } HALF] ${ key } : ${ value } ` ) ;
207+ console . log ( `[${ key <= 45 ? 'FIRST' : 'SECOND' } HALF] ${ key } : ${ value } ` ) ;
208+
209+ // Coding Challenge 4
210+
211+ /*
212+ Write a program that receives a list of variable names written in underscore_case
213+ and convert them to camelCase.
214+ The input will come from a textarea inserted into the DOM (see code below to
215+ insert the elements), and conversion will happen when the button is pressed.
216+ Test data (pasted to textarea, including spaces):
217+ underscore_case
218+ first_name
219+ Some_Variable
220+ calculate_AGE
221+ delayed_departure
222+ Should produce this output (5 separate console.log outputs):
223+ underscoreCase ✅
224+ firstName ✅✅
225+ someVariable ✅✅✅
226+ calculateAge ✅✅✅✅
227+ delayedDeparture ✅✅✅✅✅
228+
229+ Hints:
230+ § Remember which character defines a new line in the textarea 😉
231+ § The solution only needs to work for a variable made out of 2 words, like a_b
232+ § Start without worrying about the ✅. Tackle that only after you have the variable
233+ name conversion working 😉
234+ § This challenge is difficult on purpose, so start watching the solution in case
235+ you're stuck. Then pause and continue!
236+ Afterwards, test with your own test data!
237+ GOOD LUCK 😀
238+ */
239+
240+ document . body . append ( document . createElement ( 'textarea' ) ) ;
241+ document . body . append ( document . createElement ( 'button' ) ) ;
242+
243+
244+ const textArea = document . querySelector ( 'textarea' ) ;
245+ const button = document . querySelector ( 'button' ) ;
246+
247+ button . addEventListener ( 'click' , ( ) =>
248+ {
249+ const params = textArea . value . split ( '\n' ) ;
250+ const modParams = [ ] ;
251+
252+ for ( let param of params )
253+ {
254+ param = param . toLowerCase ( ) ;
255+
256+ const words = param . split ( '_' ) ;
257+ const modWords = [ ] ;
258+
259+ modWords . push ( words [ 0 ] ) ;
260+
261+ for ( let i = 1 ; i < words . length ; i ++ )
262+ modWords . push ( words [ i ] [ 0 ] . toUpperCase ( ) + words [ i ] . slice ( 1 ) ) ;
263+
264+ modParams . push ( modWords . join ( '' ) ) ;
265+ }
266+
267+ let msg = '' ;
268+ for ( let i = 0 ; i < modParams . length ; i ++ )
269+ msg = msg . concat ( `${ modParams [ i ] } ${ '✅' . repeat ( i + 1 ) } \n` ) ;
270+
271+ console . log ( msg ) ;
272+ } ) ;
0 commit comments