Skip to content

Commit 6533fab

Browse files
committed
draft an approach for poetry-club-door-policy
1 parent d870cef commit 6533fab

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Title TBA
2+
3+
```js
4+
export function frontDoorResponse(line) {
5+
return line[0];
6+
}
7+
8+
export function frontDoorPassword(word) {
9+
return word[0].toUpperCase() + word.slice(1).toLowerCase();
10+
}
11+
12+
export function backDoorResponse(line) {
13+
const trimmed = line.trim();
14+
return trimmed[trimmed.length - 1];
15+
}
16+
17+
export function backDoorPassword(word) {
18+
return frontDoorPassword(word) + ', please';
19+
}
20+
```
21+
22+
In this approach you use string indexing, string length, and various string methods to return the correct passwords.
23+
24+
## Different ways to implement `frontDoorPassword`
25+
26+
For `frontDoorPassword`, there are a variety of ways to make strings uppercase and lowercase.
27+
28+
One such way is [`toUpperCase`][mdn-to-upper-case], the method used in the code sample above.
29+
30+
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)`
31+
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.
33+
34+
A final method is to use [`toLocaleUpperCase`][mdn-to-locale-lower-case], which is very similar to `toUpperCase`.
35+
36+
When using `toLocaleUpperCase`, you can specify a locale string as an argument, but otherwise functions similarly to `toUpperCase`.
37+
38+
All of these different ways can be applied to `toLowerCase` with minimal adjustments.
39+
40+
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.
41+
42+
## Different ways to implement `backDoorResponse`
43+
44+
There are many ways to go about trimming whitespace for `backDoorResponse`.
45+
46+
The built-in string method [`trim`][mdn-trim], trims any leading and trailing whitespace from a string. This is a fairly standard solution.
47+
48+
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.
49+
50+
Another option is to use [`replaceAll`][mdn-replace-all] with `' '` as the argument to remove all whitespace in the string.
51+
52+
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.
53+
54+
Similarly, both [`replace`][mdn-replace] and `replaceAll` could be used with a regex that has the global flag for the same effect.
55+
56+
Lastly, you could not even bother to trim the whitespace at all and just use a loop:
57+
58+
```js
59+
for (let index = line.length - 1; index >= 0; index--) {
60+
if (line[index] != ' ') {
61+
return line[index];
62+
};
63+
};
64+
```
65+
66+
## Different ways to get a character from a string
67+
68+
In various parts of the code sample above, we use `[index]` access to get a character from a string.
69+
70+
However, there are other methods of doing this as well.
71+
72+
One other way is to use [`charAt`][mdn-char-at], which is very similar to `[index]` access.
73+
74+
Another method is [`at`][mdn-at], which is the same as `[index]` access, except it accepts negative numbers.
75+
76+
A negative number will count backwards from the end of the string, unlike positive numbers, which count forwards from the start.
77+
78+
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.
79+
80+
An important distiction is that `slice` accepts negative numbers like `at` does, but `substring` does not.
81+
82+
[mdn-to-upper-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
83+
[mdn-from-char-code]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode
84+
[mdn-char-code-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
85+
[mdn-to-lower-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase
86+
[mdn-to-locale-lower-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase
87+
[mdn-trim]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim
88+
[mdn-trim-end]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd
89+
[mdn-replace-all]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
90+
[mdn-replace]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
91+
[mdn-char-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt
92+
[mdn-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/at
93+
[mdn-substring]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
94+
[mdn-slice]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice

exercises/concept/poetry-club-door-policy/.approaches/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"uuid": "8e9f4077-4cbc-4111-b27e-0299a4f60e97",
55
"slug": "approach-1",
66
"title": "Title TBA",
7-
"blurb": "Blurb TBA.",
7+
"blurb": "Use string indexing, string length, and various string methods to return the correct passwords.",
88
"authors": [
99
"Yrahcaz7"
1010
]

0 commit comments

Comments
 (0)