-
-
Notifications
You must be signed in to change notification settings - Fork 642
Add an approach to Poetry Club Door Policy #2630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d870cef
Create config.json
Yrahcaz7 6533fab
draft an approach for poetry-club-door-policy
Yrahcaz7 c3f07f6
convert new approach into introduction
Yrahcaz7 b21da95
[CI] Format code
github-actions[bot] b382107
Trigger builds
Yrahcaz7 908e708
Apply suggestions from code review
Yrahcaz7 118900c
[CI] Format code
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
exercises/concept/poetry-club-door-policy/.approaches/config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"introduction": { | ||
"authors": [ | ||
"Yrahcaz7" | ||
] | ||
} | ||
} |
166 changes: 166 additions & 0 deletions
166
exercises/concept/poetry-club-door-policy/.approaches/introduction.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
# 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`, 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' | ||
|
||
str.toLocaleUpperCase('tr'); | ||
// => 'İSTANBUL' | ||
``` | ||
|
||
### 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.