From d870cef8a3536d9759e5c12f97ffeead56e95916 Mon Sep 17 00:00:00 2001 From: Yrahcaz <74512479+Yrahcaz7@users.noreply.github.com> Date: Wed, 21 May 2025 10:50:24 -0400 Subject: [PATCH 1/7] Create config.json --- .../poetry-club-door-policy/.approaches/config.json | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 exercises/concept/poetry-club-door-policy/.approaches/config.json diff --git a/exercises/concept/poetry-club-door-policy/.approaches/config.json b/exercises/concept/poetry-club-door-policy/.approaches/config.json new file mode 100644 index 0000000000..2a38a45509 --- /dev/null +++ b/exercises/concept/poetry-club-door-policy/.approaches/config.json @@ -0,0 +1,13 @@ +{ + "approaches": [ + { + "uuid": "8e9f4077-4cbc-4111-b27e-0299a4f60e97", + "slug": "approach-1", + "title": "Title TBA", + "blurb": "Blurb TBA.", + "authors": [ + "Yrahcaz7" + ] + } + ] +} From 6533fabc470268690e3774d7adefdbeb160b8458 Mon Sep 17 00:00:00 2001 From: Yrahcaz7 Date: Wed, 21 May 2025 12:30:48 -0400 Subject: [PATCH 2/7] draft an approach for poetry-club-door-policy --- .../.approaches/approach-1/content.md | 94 +++++++++++++++++++ .../.approaches/config.json | 2 +- 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 exercises/concept/poetry-club-door-policy/.approaches/approach-1/content.md diff --git a/exercises/concept/poetry-club-door-policy/.approaches/approach-1/content.md b/exercises/concept/poetry-club-door-policy/.approaches/approach-1/content.md new file mode 100644 index 0000000000..2ec6d3aa47 --- /dev/null +++ b/exercises/concept/poetry-club-door-policy/.approaches/approach-1/content.md @@ -0,0 +1,94 @@ +# Title TBA + +```js +export function frontDoorResponse(line) { + return line[0]; +} + +export function frontDoorPassword(word) { + return word[0].toUpperCase() + word.slice(1).toLowerCase(); +} + +export function backDoorResponse(line) { + const trimmed = line.trim(); + return trimmed[trimmed.length - 1]; +} + +export function backDoorPassword(word) { + return frontDoorPassword(word) + ', please'; +} +``` + +In this approach you use string indexing, string length, and various string methods to return the correct passwords. + +## Different ways to implement `frontDoorPassword` + +For `frontDoorPassword`, there are a variety of ways to make strings uppercase and lowercase. + +One such way is [`toUpperCase`][mdn-to-upper-case], the method used in the code sample above. + +Another way is to use [`String.fromCharCode`][mdn-from-char-code] along with [`charCodeAt`][mdn-char-code-at], like so: `String.fromCharCode(line.charCodeAt(0) - 32)` + +This method is much longer than the others and it also only works for lowercase english letters, so for it to work with uppercase input, [`toLowerCase`][mdn-to-lower-case] or additional logic is needed. + +A final method is to use [`toLocaleUpperCase`][mdn-to-locale-lower-case], which is very similar to `toUpperCase`. + +When using `toLocaleUpperCase`, you can specify a locale string as an argument, but otherwise functions similarly to `toUpperCase`. + +All of these different ways can be applied to `toLowerCase` with minimal adjustments. + +The `String.fromCharCode` way introduces unnecessary complexity into your code, while both the `toUpperCase` and `toLocaleUpperCase` methods are fairly concise, so those two methods are generally the preferred ways. + +## Different ways to implement `backDoorResponse` + +There are many ways to go about trimming whitespace for `backDoorResponse`. + +The built-in string method [`trim`][mdn-trim], trims any leading and trailing whitespace from a string. This is a fairly standard solution. + +The string method [`trimEnd`][mdn-trim-end] can also be used, as we only need to trim trailing whitespace to get the last non-whitespace character. + +Another option is to use [`replaceAll`][mdn-replace-all] with `' '` as the argument to remove all whitespace in the string. + +This would merge all the words in the string into one, but that doesn't matter here as we only care about the last character and not the rest of the string. + +Similarly, both [`replace`][mdn-replace] and `replaceAll` could be used with a regex that has the global flag for the same effect. + +Lastly, you could not even bother to trim the whitespace at all and just use a loop: + +```js +for (let index = line.length - 1; index >= 0; index--) { + if (line[index] != ' ') { + return line[index]; + }; +}; +``` + +## Different ways to get a character from a string + +In various parts of the code sample above, we use `[index]` access to get a character from a string. + +However, there are other methods of doing this as well. + +One other way is to use [`charAt`][mdn-char-at], which is very similar to `[index]` access. + +Another method is [`at`][mdn-at], which is the same as `[index]` access, except it accepts negative numbers. + +A negative number will count backwards from the end of the string, unlike positive numbers, which count forwards from the start. + +In addition, [`substring`][mdn-substring] and [`slice`][mdn-slice] can be used. These string methods are normally used to get portions of strings, rather than a single character. + +An important distiction is that `slice` accepts negative numbers like `at` does, but `substring` does not. + +[mdn-to-upper-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase +[mdn-from-char-code]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode +[mdn-char-code-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt +[mdn-to-lower-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase +[mdn-to-locale-lower-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase +[mdn-trim]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim +[mdn-trim-end]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd +[mdn-replace-all]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll +[mdn-replace]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace +[mdn-char-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt +[mdn-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/at +[mdn-substring]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring +[mdn-slice]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice diff --git a/exercises/concept/poetry-club-door-policy/.approaches/config.json b/exercises/concept/poetry-club-door-policy/.approaches/config.json index 2a38a45509..edb9cf3299 100644 --- a/exercises/concept/poetry-club-door-policy/.approaches/config.json +++ b/exercises/concept/poetry-club-door-policy/.approaches/config.json @@ -4,7 +4,7 @@ "uuid": "8e9f4077-4cbc-4111-b27e-0299a4f60e97", "slug": "approach-1", "title": "Title TBA", - "blurb": "Blurb TBA.", + "blurb": "Use string indexing, string length, and various string methods to return the correct passwords.", "authors": [ "Yrahcaz7" ] From c3f07f6d4bb7ac7852e1939288dd5b93e3c48581 Mon Sep 17 00:00:00 2001 From: Yrahcaz7 Date: Wed, 21 May 2025 15:09:25 -0400 Subject: [PATCH 3/7] convert new approach into introduction --- .../.approaches/approach-1/content.md | 94 ----------- .../.approaches/config.json | 16 +- .../.approaches/introduction.md | 146 ++++++++++++++++++ 3 files changed, 151 insertions(+), 105 deletions(-) delete mode 100644 exercises/concept/poetry-club-door-policy/.approaches/approach-1/content.md create mode 100644 exercises/concept/poetry-club-door-policy/.approaches/introduction.md diff --git a/exercises/concept/poetry-club-door-policy/.approaches/approach-1/content.md b/exercises/concept/poetry-club-door-policy/.approaches/approach-1/content.md deleted file mode 100644 index 2ec6d3aa47..0000000000 --- a/exercises/concept/poetry-club-door-policy/.approaches/approach-1/content.md +++ /dev/null @@ -1,94 +0,0 @@ -# Title TBA - -```js -export function frontDoorResponse(line) { - return line[0]; -} - -export function frontDoorPassword(word) { - return word[0].toUpperCase() + word.slice(1).toLowerCase(); -} - -export function backDoorResponse(line) { - const trimmed = line.trim(); - return trimmed[trimmed.length - 1]; -} - -export function backDoorPassword(word) { - return frontDoorPassword(word) + ', please'; -} -``` - -In this approach you use string indexing, string length, and various string methods to return the correct passwords. - -## Different ways to implement `frontDoorPassword` - -For `frontDoorPassword`, there are a variety of ways to make strings uppercase and lowercase. - -One such way is [`toUpperCase`][mdn-to-upper-case], the method used in the code sample above. - -Another way is to use [`String.fromCharCode`][mdn-from-char-code] along with [`charCodeAt`][mdn-char-code-at], like so: `String.fromCharCode(line.charCodeAt(0) - 32)` - -This method is much longer than the others and it also only works for lowercase english letters, so for it to work with uppercase input, [`toLowerCase`][mdn-to-lower-case] or additional logic is needed. - -A final method is to use [`toLocaleUpperCase`][mdn-to-locale-lower-case], which is very similar to `toUpperCase`. - -When using `toLocaleUpperCase`, you can specify a locale string as an argument, but otherwise functions similarly to `toUpperCase`. - -All of these different ways can be applied to `toLowerCase` with minimal adjustments. - -The `String.fromCharCode` way introduces unnecessary complexity into your code, while both the `toUpperCase` and `toLocaleUpperCase` methods are fairly concise, so those two methods are generally the preferred ways. - -## Different ways to implement `backDoorResponse` - -There are many ways to go about trimming whitespace for `backDoorResponse`. - -The built-in string method [`trim`][mdn-trim], trims any leading and trailing whitespace from a string. This is a fairly standard solution. - -The string method [`trimEnd`][mdn-trim-end] can also be used, as we only need to trim trailing whitespace to get the last non-whitespace character. - -Another option is to use [`replaceAll`][mdn-replace-all] with `' '` as the argument to remove all whitespace in the string. - -This would merge all the words in the string into one, but that doesn't matter here as we only care about the last character and not the rest of the string. - -Similarly, both [`replace`][mdn-replace] and `replaceAll` could be used with a regex that has the global flag for the same effect. - -Lastly, you could not even bother to trim the whitespace at all and just use a loop: - -```js -for (let index = line.length - 1; index >= 0; index--) { - if (line[index] != ' ') { - return line[index]; - }; -}; -``` - -## Different ways to get a character from a string - -In various parts of the code sample above, we use `[index]` access to get a character from a string. - -However, there are other methods of doing this as well. - -One other way is to use [`charAt`][mdn-char-at], which is very similar to `[index]` access. - -Another method is [`at`][mdn-at], which is the same as `[index]` access, except it accepts negative numbers. - -A negative number will count backwards from the end of the string, unlike positive numbers, which count forwards from the start. - -In addition, [`substring`][mdn-substring] and [`slice`][mdn-slice] can be used. These string methods are normally used to get portions of strings, rather than a single character. - -An important distiction is that `slice` accepts negative numbers like `at` does, but `substring` does not. - -[mdn-to-upper-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase -[mdn-from-char-code]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode -[mdn-char-code-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt -[mdn-to-lower-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase -[mdn-to-locale-lower-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase -[mdn-trim]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim -[mdn-trim-end]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd -[mdn-replace-all]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll -[mdn-replace]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace -[mdn-char-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt -[mdn-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/at -[mdn-substring]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring -[mdn-slice]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice diff --git a/exercises/concept/poetry-club-door-policy/.approaches/config.json b/exercises/concept/poetry-club-door-policy/.approaches/config.json index edb9cf3299..0b90d0d0dc 100644 --- a/exercises/concept/poetry-club-door-policy/.approaches/config.json +++ b/exercises/concept/poetry-club-door-policy/.approaches/config.json @@ -1,13 +1,7 @@ { - "approaches": [ - { - "uuid": "8e9f4077-4cbc-4111-b27e-0299a4f60e97", - "slug": "approach-1", - "title": "Title TBA", - "blurb": "Use string indexing, string length, and various string methods to return the correct passwords.", - "authors": [ - "Yrahcaz7" - ] - } - ] + "introduction": { + "authors": [ + "Yrahcaz7" + ] + } } diff --git a/exercises/concept/poetry-club-door-policy/.approaches/introduction.md b/exercises/concept/poetry-club-door-policy/.approaches/introduction.md new file mode 100644 index 0000000000..f2289f15d4 --- /dev/null +++ b/exercises/concept/poetry-club-door-policy/.approaches/introduction.md @@ -0,0 +1,146 @@ +# Introduction + +There are various ways to solve each part of Poetry Club Door Policy. A commonality between most of the parts is needing to get a character from the provided string. + +There are multiple ways to do this, one of which is the standard way of using `[index]` access. + +One other way is to use [`charAt`][mdn-char-at], which is the same as `[index]` access for most purposes. + +Another method is [`at`][mdn-at], which is the same as `[index]` access, except it accepts negative numbers. + +A negative number will count backwards from the end of the string, unlike positive numbers, which count forwards from the start. + +In addition, [`substring`][mdn-substring] and [`slice`][mdn-slice] can be used. These string methods are normally used to get portions of strings, rather than a single character. + +An important distiction is that `slice` accepts negative numbers like `at` does, but `substring` does not. + +## Different ways to implement `frontDoorPassword` + +For `frontDoorPassword`, there are a variety of ways to make strings uppercase and lowercase. + +### Approach: `toUpperCase` and `toLowerCase` + +```js +export function frontDoorPassword(word) { + return word[0].toUpperCase() + word.slice(1).toLowerCase(); +} +``` + +This approach is a standard method that uses [`toUpperCase`][mdn-to-upper-case] and [`toLowerCase`][mdn-to-lower-case]. + +### Approach: `toLocaleUpperCase` and `toLocaleLowerCase` + +```js +export function frontDoorPassword(word) { + return word[0].toLocaleUpperCase() + word.substring(1).toLocaleLowerCase(); +} +``` + +This approach uses [`toLocaleUpperCase`][mdn-to-locale-upper-case] and [`toLocaleLowerCase`][mdn-to-locale-lower-case], which are very similar to `toUpperCase` and `toLowerCase`. + +When using `toLocaleUpperCase` or `toLocaleLowerCase`, you can specify a locale string as an argument and the output will be adjusted to that locale. + +### Approach: `String.fromCharCode` and `charCodeAt` + +```js +export function frontDoorPassword(word) { + let charCode = word.charCodeAt(0); + if (charCode >= 97) charCode -= 32; + let password = String.fromCharCode(charCode); + for (let index = 1; index < word.length; index++) { + charCode = word.charCodeAt(index); + if (charCode <= 90) charCode += 32; + password += String.fromCharCode(charCode); + }; + return password; +} +``` + +This approach uses [`String.fromCharCode`][mdn-from-char-code] along with [`charCodeAt`][mdn-char-code-at]. + +This method is much longer than the others and it only works with english letters, so it is less than ideal. + +## Different ways to implement `backDoorResponse` + +There are many ways to go about trimming whitespace for `backDoorResponse`. + +### Approach: `trim` and `[index]` access + +```js +export function backDoorResponse(line) { + const trimmed = line.trim(); + return trimmed[trimmed.length - 1]; +} +``` + +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. + +### Approach: `trimEnd` and `at` + +```js +export function backDoorResponse(line) { + return line.trimEnd().at(-1); +} +``` + +This approach uses the string method [`trimEnd`][mdn-trim-end], which only trims trailing whitespace, unlike `trim`. + +It also uses `at` instead of `[index]` access make the solution shorter. + +### Approach: `replaceAll` and `charAt` + +```js +export function backDoorResponse(line) { + const trimmed = line.replaceAll(' ', ''); + return trimmed.charAt(trimmed.length - 1); +} +``` + +This approach uses [`replaceAll`][mdn-replace-all] to remove all of the spaces in the string. + +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. + +### Approach: `replace` and literal `RegExp` + +```js +export function backDoorResponse(line) { + const trimmed = line.replace(/\s/g, ''); + return trimmed[trimmed.length - 1]; +} +``` + +This approach uses [`replace`][mdn-replace] with a [regular expression literal][mdn-regular-expressions], achieving a similar result to the previous approach. + +The main difference is that the previous approach only removes spaces, while this approach can remove any type of whitespace. + +### Approach: `for` loop + +```js +export function backDoorResponse(line) { + for (let index = line.length - 1; index >= 0; index--) { + if (line[index] != ' ') { + return line[index]; + }; + }; + return ''; +} +``` + +This approach does not trim whitespace. Instead, it uses a [for loop][mdn-for] to return the first character that is not a space from the end of the string. + +[mdn-char-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt +[mdn-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/at +[mdn-substring]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring +[mdn-slice]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice +[mdn-to-upper-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase +[mdn-to-lower-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase +[mdn-to-locale-upper-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase +[mdn-to-locale-lower-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase +[mdn-from-char-code]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode +[mdn-char-code-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt +[mdn-trim]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim +[mdn-trim-end]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd +[mdn-replace-all]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll +[mdn-replace]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace +[mdn-regular-expressions]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions +[mdn-for]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for From b21da955f044d2ec9f5e9cf9c6fce2f0834f8163 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 21 May 2025 20:02:56 +0000 Subject: [PATCH 4/7] [CI] Format code --- .../poetry-club-door-policy/.approaches/introduction.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exercises/concept/poetry-club-door-policy/.approaches/introduction.md b/exercises/concept/poetry-club-door-policy/.approaches/introduction.md index f2289f15d4..636090ff5f 100644 --- a/exercises/concept/poetry-club-door-policy/.approaches/introduction.md +++ b/exercises/concept/poetry-club-door-policy/.approaches/introduction.md @@ -51,7 +51,7 @@ export function frontDoorPassword(word) { charCode = word.charCodeAt(index); if (charCode <= 90) charCode += 32; password += String.fromCharCode(charCode); - }; + } return password; } ``` @@ -120,8 +120,8 @@ export function backDoorResponse(line) { for (let index = line.length - 1; index >= 0; index--) { if (line[index] != ' ') { return line[index]; - }; - }; + } + } return ''; } ``` From b382107a1676c582df47b80a4bc1cf4f6821b080 Mon Sep 17 00:00:00 2001 From: Yrahcaz7 Date: Wed, 21 May 2025 16:13:21 -0400 Subject: [PATCH 5/7] Trigger builds From 908e708b5162ae24f2f97b8a1ae97b355d431388 Mon Sep 17 00:00:00 2001 From: Yrahcaz <74512479+Yrahcaz7@users.noreply.github.com> Date: Wed, 21 May 2025 20:28:17 -0400 Subject: [PATCH 6/7] Apply suggestions from code review Co-authored-by: Derk-Jan Karrenbeld --- .../.approaches/introduction.md | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/exercises/concept/poetry-club-door-policy/.approaches/introduction.md b/exercises/concept/poetry-club-door-policy/.approaches/introduction.md index 636090ff5f..c2a6829193 100644 --- a/exercises/concept/poetry-club-door-policy/.approaches/introduction.md +++ b/exercises/concept/poetry-club-door-policy/.approaches/introduction.md @@ -1,16 +1,17 @@ # Introduction -There are various ways to solve each part of Poetry Club Door Policy. A commonality between most of the parts is needing to get a character from the provided string. +There are various ways to solve each part of Poetry Club Door Policy. +A commonality between most of the parts is needing to get a character from the provided string. There are multiple ways to do this, one of which is the standard way of using `[index]` access. One other way is to use [`charAt`][mdn-char-at], which is the same as `[index]` access for most purposes. Another method is [`at`][mdn-at], which is the same as `[index]` access, except it accepts negative numbers. - A negative number will count backwards from the end of the string, unlike positive numbers, which count forwards from the start. -In addition, [`substring`][mdn-substring] and [`slice`][mdn-slice] can be used. These string methods are normally used to get portions of strings, rather than a single character. +In addition, [`substring`][mdn-substring] and [`slice`][mdn-slice] can be used. +These string methods are normally used to get portions of strings, rather than a single character. An important distiction is that `slice` accepts negative numbers like `at` does, but `substring` does not. @@ -36,20 +37,38 @@ export function frontDoorPassword(word) { } ``` -This approach uses [`toLocaleUpperCase`][mdn-to-locale-upper-case] and [`toLocaleLowerCase`][mdn-to-locale-lower-case], which are very similar to `toUpperCase` and `toLowerCase`. +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. +This approach is necessary when the language locale has a non-standard mapping between lower and uppercase. + +```javascript +const str = "istanbul"; + +str.toUpperCase() +// => 'ISTANBUL' +str.toLocaleUpperCase('en-US') +// => 'ISTANBUL' -When using `toLocaleUpperCase` or `toLocaleLowerCase`, you can specify a locale string as an argument and the output will be adjusted to that locale. +str.toLocaleUpperCase('tr') +// => 'İSTANBUL' +``` ### Approach: `String.fromCharCode` and `charCodeAt` ```js export function frontDoorPassword(word) { let charCode = word.charCodeAt(0); - if (charCode >= 97) charCode -= 32; + if (charCode >= 97) { + charCode -= 32; + } + let password = String.fromCharCode(charCode); + for (let index = 1; index < word.length; index++) { charCode = word.charCodeAt(index); - if (charCode <= 90) charCode += 32; + if (charCode <= 90) { + charCode += 32; + } + password += String.fromCharCode(charCode); } return password; @@ -126,7 +145,8 @@ export function backDoorResponse(line) { } ``` -This approach does not trim whitespace. Instead, it uses a [for loop][mdn-for] to return the first character that is not a space from the end of the string. +This approach does not trim whitespace. +Instead, it uses a [for loop][mdn-for] to return the first character that is not a space from the end of the string. [mdn-char-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt [mdn-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/at From 118900cbc7f8d2d6d73225fd8efebcdbabf3e06f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 22 May 2025 00:51:42 +0000 Subject: [PATCH 7/7] [CI] Format code --- .../.approaches/introduction.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/exercises/concept/poetry-club-door-policy/.approaches/introduction.md b/exercises/concept/poetry-club-door-policy/.approaches/introduction.md index c2a6829193..520930c6d1 100644 --- a/exercises/concept/poetry-club-door-policy/.approaches/introduction.md +++ b/exercises/concept/poetry-club-door-policy/.approaches/introduction.md @@ -41,14 +41,14 @@ This approach uses [`toLocaleUpperCase`][mdn-to-locale-upper-case] and [`toLocal This approach is necessary when the language locale has a non-standard mapping between lower and uppercase. ```javascript -const str = "istanbul"; +const str = 'istanbul'; -str.toUpperCase() +str.toUpperCase(); // => 'ISTANBUL' -str.toLocaleUpperCase('en-US') +str.toLocaleUpperCase('en-US'); // => 'ISTANBUL' -str.toLocaleUpperCase('tr') +str.toLocaleUpperCase('tr'); // => 'İSTANBUL' ``` @@ -60,15 +60,15 @@ export function frontDoorPassword(word) { if (charCode >= 97) { charCode -= 32; } - + let password = String.fromCharCode(charCode); - + for (let index = 1; index < word.length; index++) { charCode = word.charCodeAt(index); if (charCode <= 90) { charCode += 32; } - + password += String.fromCharCode(charCode); } return password;