@@ -83,23 +83,58 @@ Assign the result to a variable named swappedString.
8383
8484*/
8585
86- //Starter Code
87- // Task 1
86+ // --------------------------------------------------
87+ // Practice Problem #1
88+ // --------------------------------------------------
89+
90+ // Exercise 1: Retrieve and Add Two Code Points
91+ let inputString = "Hi" ;
92+ let sumCodePoints = inputString . charCodeAt ( 0 ) + inputString . charCodeAt ( 1 ) ; // 'H' = 72, 'i' = 105 → 177
93+
94+ // Exercise 2: Generate a String from Two Code Points
95+ let codePoint1 = 65 ;
96+ let codePoint2 = 66 ;
97+ let combinedString = String . fromCharCode ( codePoint1 ) + String . fromCharCode ( codePoint2 ) ; // "AB"
98+
99+ // Exercise 3: Find the Character Difference
100+ let diffString = "Cat" ;
101+ let index1 = 0 ;
102+ let index2 = 2 ;
103+ let codePointDifference = Math . abs ( diffString . charCodeAt ( index1 ) - diffString . charCodeAt ( index2 ) ) ; // |67 - 116| = 49
104+
105+ // --------------------------------------------------
106+ // Practice Problem #2
107+ // --------------------------------------------------
108+
109+ // Task 1: Extract Code Points from Characters
88110let inputString1 = "Code" ;
89- let firstCodePoint ; // Your code here
90- let thirdCodePoint ; // Your code here
111+ let firstCodePoint = inputString1 . charCodeAt ( 0 ) ; // 'C' = 67
112+ let thirdCodePoint = inputString1 . charCodeAt ( 2 ) ; // 'd' = 100
91113
92- // Task 2
93- let wordFromCodePoints ; // Your code here
114+ // Task 2: Create a Word from Code Points
115+ let wordFromCodePoints = String . fromCharCode ( 72 , 101 , 108 , 108 ) ; // "Hell"
94116
95- // Task 3
117+ // Task 3: Swap First and Last Characters
96118let inputString2 = "Launch" ;
97- let swappedString ; // Your code here
98-
99- // Log all results
100- console . log ( {
101- firstCodePoint,
102- thirdCodePoint,
103- wordFromCodePoints,
104- swappedString,
105- } ) ;
119+ let firstCharCode = inputString2 . charCodeAt ( 0 ) ; // 'L'
120+ let lastCharCode = inputString2 . charCodeAt ( inputString2 . length - 1 ) ; // 'h'
121+
122+ let swappedString =
123+ String . fromCharCode ( lastCharCode ) +
124+ inputString2 . slice ( 1 , - 1 ) +
125+ String . fromCharCode ( firstCharCode ) ;
126+
127+ // --------------------------------------------------
128+ // Log All Results
129+ // --------------------------------------------------
130+
131+ console . log ( "----- Practice Problem #1 -----" ) ;
132+ console . log ( "sumCodePoints:" , sumCodePoints ) ; // 177
133+ console . log ( "combinedString:" , combinedString ) ; // "AB"
134+ console . log ( "codePointDifference:" , codePointDifference ) ; // 49
135+
136+ console . log ( "\n----- Practice Problem #2 -----" ) ;
137+ console . log ( "firstCodePoint:" , firstCodePoint ) ; // 67
138+ console . log ( "thirdCodePoint:" , thirdCodePoint ) ; // 100
139+ console . log ( "wordFromCodePoints:" , wordFromCodePoints ) ; // "Hell"
140+ console . log ( "swappedString:" , swappedString ) ; // "hauncL"
0 commit comments