|
| 1 | +# Introduction |
| 2 | + |
| 3 | +There are various ways to solve each part of Poetry Club Door Policy. |
| 4 | +A commonality between most of the parts is needing to get a character from the provided string. |
| 5 | + |
| 6 | +There are multiple ways to do this, one of which is the standard way of using `[index]` access. |
| 7 | + |
| 8 | +One other way is to use [`charAt`][mdn-char-at], which is the same as `[index]` access for most purposes. |
| 9 | + |
| 10 | +Another method is [`at`][mdn-at], which is the same as `[index]` access, except it accepts negative numbers. |
| 11 | +A negative number will count backwards from the end of the string, unlike positive numbers, which count forwards from the start. |
| 12 | + |
| 13 | +In addition, [`substring`][mdn-substring] and [`slice`][mdn-slice] can be used. |
| 14 | +These string methods are normally used to get portions of strings, rather than a single character. |
| 15 | + |
| 16 | +An important distiction is that `slice` accepts negative numbers like `at` does, but `substring` does not. |
| 17 | + |
| 18 | +## Different ways to implement `frontDoorPassword` |
| 19 | + |
| 20 | +For `frontDoorPassword`, there are a variety of ways to make strings uppercase and lowercase. |
| 21 | + |
| 22 | +### Approach: `toUpperCase` and `toLowerCase` |
| 23 | + |
| 24 | +```js |
| 25 | +export function frontDoorPassword(word) { |
| 26 | + return word[0].toUpperCase() + word.slice(1).toLowerCase(); |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +This approach is a standard method that uses [`toUpperCase`][mdn-to-upper-case] and [`toLowerCase`][mdn-to-lower-case]. |
| 31 | + |
| 32 | +### Approach: `toLocaleUpperCase` and `toLocaleLowerCase` |
| 33 | + |
| 34 | +```js |
| 35 | +export function frontDoorPassword(word) { |
| 36 | + return word[0].toLocaleUpperCase() + word.substring(1).toLocaleLowerCase(); |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +This approach uses [`toLocaleUpperCase`][mdn-to-locale-upper-case] and [`toLocaleLowerCase`][mdn-to-locale-lower-case], which are very similar to `toUpperCase` and `toLowerCase`, but work with either the current locale or a given locale, which can be specified as an argument. |
| 41 | +This approach is necessary when the language locale has a non-standard mapping between lower and uppercase. |
| 42 | + |
| 43 | +```javascript |
| 44 | +const str = 'istanbul'; |
| 45 | + |
| 46 | +str.toUpperCase(); |
| 47 | +// => 'ISTANBUL' |
| 48 | +str.toLocaleUpperCase('en-US'); |
| 49 | +// => 'ISTANBUL' |
| 50 | + |
| 51 | +str.toLocaleUpperCase('tr'); |
| 52 | +// => 'İSTANBUL' |
| 53 | +``` |
| 54 | + |
| 55 | +### Approach: `String.fromCharCode` and `charCodeAt` |
| 56 | + |
| 57 | +```js |
| 58 | +export function frontDoorPassword(word) { |
| 59 | + let charCode = word.charCodeAt(0); |
| 60 | + if (charCode >= 97) { |
| 61 | + charCode -= 32; |
| 62 | + } |
| 63 | + |
| 64 | + let password = String.fromCharCode(charCode); |
| 65 | + |
| 66 | + for (let index = 1; index < word.length; index++) { |
| 67 | + charCode = word.charCodeAt(index); |
| 68 | + if (charCode <= 90) { |
| 69 | + charCode += 32; |
| 70 | + } |
| 71 | + |
| 72 | + password += String.fromCharCode(charCode); |
| 73 | + } |
| 74 | + return password; |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +This approach uses [`String.fromCharCode`][mdn-from-char-code] along with [`charCodeAt`][mdn-char-code-at]. |
| 79 | + |
| 80 | +This method is much longer than the others and it only works with english letters, so it is less than ideal. |
| 81 | + |
| 82 | +## Different ways to implement `backDoorResponse` |
| 83 | + |
| 84 | +There are many ways to go about trimming whitespace for `backDoorResponse`. |
| 85 | + |
| 86 | +### Approach: `trim` and `[index]` access |
| 87 | + |
| 88 | +```js |
| 89 | +export function backDoorResponse(line) { |
| 90 | + const trimmed = line.trim(); |
| 91 | + return trimmed[trimmed.length - 1]; |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +This standard approach uses `[index]` access and the built-in string method [`trim`][mdn-trim], which trims any leading and trailing whitespace from a string. |
| 96 | + |
| 97 | +### Approach: `trimEnd` and `at` |
| 98 | + |
| 99 | +```js |
| 100 | +export function backDoorResponse(line) { |
| 101 | + return line.trimEnd().at(-1); |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +This approach uses the string method [`trimEnd`][mdn-trim-end], which only trims trailing whitespace, unlike `trim`. |
| 106 | + |
| 107 | +It also uses `at` instead of `[index]` access make the solution shorter. |
| 108 | + |
| 109 | +### Approach: `replaceAll` and `charAt` |
| 110 | + |
| 111 | +```js |
| 112 | +export function backDoorResponse(line) { |
| 113 | + const trimmed = line.replaceAll(' ', ''); |
| 114 | + return trimmed.charAt(trimmed.length - 1); |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +This approach uses [`replaceAll`][mdn-replace-all] to remove all of the spaces in the string. |
| 119 | + |
| 120 | +This merges all the words in the string together, but that doesn't matter here as we only care about the last character and not the rest of the string. |
| 121 | + |
| 122 | +### Approach: `replace` and literal `RegExp` |
| 123 | + |
| 124 | +```js |
| 125 | +export function backDoorResponse(line) { |
| 126 | + const trimmed = line.replace(/\s/g, ''); |
| 127 | + return trimmed[trimmed.length - 1]; |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +This approach uses [`replace`][mdn-replace] with a [regular expression literal][mdn-regular-expressions], achieving a similar result to the previous approach. |
| 132 | + |
| 133 | +The main difference is that the previous approach only removes spaces, while this approach can remove any type of whitespace. |
| 134 | + |
| 135 | +### Approach: `for` loop |
| 136 | + |
| 137 | +```js |
| 138 | +export function backDoorResponse(line) { |
| 139 | + for (let index = line.length - 1; index >= 0; index--) { |
| 140 | + if (line[index] != ' ') { |
| 141 | + return line[index]; |
| 142 | + } |
| 143 | + } |
| 144 | + return ''; |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +This approach does not trim whitespace. |
| 149 | +Instead, it uses a [for loop][mdn-for] to return the first character that is not a space from the end of the string. |
| 150 | + |
| 151 | +[mdn-char-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt |
| 152 | +[mdn-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/at |
| 153 | +[mdn-substring]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring |
| 154 | +[mdn-slice]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice |
| 155 | +[mdn-to-upper-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase |
| 156 | +[mdn-to-lower-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase |
| 157 | +[mdn-to-locale-upper-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase |
| 158 | +[mdn-to-locale-lower-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase |
| 159 | +[mdn-from-char-code]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode |
| 160 | +[mdn-char-code-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt |
| 161 | +[mdn-trim]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim |
| 162 | +[mdn-trim-end]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd |
| 163 | +[mdn-replace-all]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll |
| 164 | +[mdn-replace]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace |
| 165 | +[mdn-regular-expressions]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions |
| 166 | +[mdn-for]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for |
0 commit comments