|
| 1 | +/* |
| 2 | +
|
| 3 | +Practice Problem #1 |
| 4 | +
|
| 5 | +Exercise 1: Retrieve and Add Two Code Points |
| 6 | +Objective: Extract the Unicode code points of the first two characters of a string, |
| 7 | +add them together, and return the result. |
| 8 | +
|
| 9 | +Instructions: |
| 10 | +Write a program that takes a string with at least two characters. |
| 11 | +Use charCodeAt to get the Unicode code points of the first two characters. |
| 12 | +Add the two code points together and assign the result to a variable named sumCodePoints. |
| 13 | +Input: |
| 14 | +let inputString = "Hi"; |
| 15 | +
|
| 16 | +Expected Output: |
| 17 | +sumCodePoints: 177 // 'H' = 72, 'i' = 105, 72 + 105 = 177 |
| 18 | +
|
| 19 | +Exercise 2: Generate a String from Two Code Points |
| 20 | +Objective: Take two numeric Unicode code points, |
| 21 | +convert them to characters using String.fromCharCode, |
| 22 | +and combine them into a single string. |
| 23 | +
|
| 24 | +Instructions: |
| 25 | +Write a program that accepts two numeric Unicode code points. |
| 26 | +Use String.fromCharCode to convert each code point to its corresponding character. |
| 27 | +Combine the two characters into a single string and assign it to a variable named combinedString. |
| 28 | +Input: |
| 29 | +let codePoint1 = 65; |
| 30 | +let codePoint2 = 66; |
| 31 | +
|
| 32 | +Expected Output: |
| 33 | +combinedString: "AB" // 65 = 'A', 66 = 'B', combined = "AB" |
| 34 | +
|
| 35 | +Exercise 3: Find the Character Difference |
| 36 | +Objective: Extract the Unicode code points of two given characters from a string |
| 37 | +and calculate the absolute difference between them. |
| 38 | +
|
| 39 | +Instructions: |
| 40 | +Write a program that takes a string with at least two characters and two specified indices. |
| 41 | +Use charCodeAt to find the Unicode code points of the characters at the specified indices. |
| 42 | +Calculate the absolute difference between the two code points and assign it to a variable named codePointDifference. |
| 43 | +Input: |
| 44 | +let inputString = "Cat"; |
| 45 | +let index1 = 0; |
| 46 | +let index2 = 2; |
| 47 | +
|
| 48 | +Expected Output: |
| 49 | +codePointDifference: 16 // 'C' = 67, 't' = 116, |67 - 116| = 16 |
| 50 | +
|
| 51 | +
|
| 52 | +
|
| 53 | +Practice Problem #2 |
| 54 | +
|
| 55 | +Objective |
| 56 | +Practice converting characters from strings to Unicode code points |
| 57 | +using charCodeAt and converting Unicode code points back to characters |
| 58 | +using String.fromCharCode. |
| 59 | +
|
| 60 | +Instructions: |
| 61 | +You will complete three tasks to explore the relationship |
| 62 | +between characters and their Unicode code points. |
| 63 | +Write JavaScript code to complete each task, |
| 64 | +ensuring you assign your results to the specified variables. |
| 65 | +Log all results to the console. |
| 66 | +
|
| 67 | +Tasks: |
| 68 | +Task 1: Extract Code Points from Characters |
| 69 | +Take the string "Code". |
| 70 | +Use charCodeAt to extract the Unicode code points of the first and third characters. |
| 71 | +Assign the results to variables named firstCodePoint and thirdCodePoint. |
| 72 | +
|
| 73 | +Task 2: Create a Word from Code Points |
| 74 | +Use the Unicode code points 72, 101, 108, and 108 (corresponding to the characters H, e, l, and l). |
| 75 | +Use String.fromCharCode to create the word "Hell". |
| 76 | +Assign the result to a variable named wordFromCodePoints. |
| 77 | +
|
| 78 | +Task 3: Swap First and Last Characters |
| 79 | +Take the string "Launch". |
| 80 | +Extract the Unicode code points of the first and last characters using charCodeAt. |
| 81 | +Use String.fromCharCode to swap these characters and create a new string: "hauncL". |
| 82 | +Assign the result to a variable named swappedString. |
| 83 | +
|
| 84 | +*/ |
| 85 | + |
| 86 | +//Starter Code |
| 87 | +// Task 1 |
| 88 | +let inputString1 = "Code"; |
| 89 | +let firstCodePoint; // Your code here |
| 90 | +let thirdCodePoint; // Your code here |
| 91 | + |
| 92 | +// Task 2 |
| 93 | +let wordFromCodePoints; // Your code here |
| 94 | + |
| 95 | +// Task 3 |
| 96 | +let 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 | +}); |
0 commit comments