Skip to content

Commit c3f07f6

Browse files
committed
convert new approach into introduction
1 parent 6533fab commit c3f07f6

File tree

3 files changed

+151
-105
lines changed

3 files changed

+151
-105
lines changed

exercises/concept/poetry-club-door-policy/.approaches/approach-1/content.md

Lines changed: 0 additions & 94 deletions
This file was deleted.
Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
{
2-
"approaches": [
3-
{
4-
"uuid": "8e9f4077-4cbc-4111-b27e-0299a4f60e97",
5-
"slug": "approach-1",
6-
"title": "Title TBA",
7-
"blurb": "Use string indexing, string length, and various string methods to return the correct passwords.",
8-
"authors": [
9-
"Yrahcaz7"
10-
]
11-
}
12-
]
2+
"introduction": {
3+
"authors": [
4+
"Yrahcaz7"
5+
]
6+
}
137
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Introduction
2+
3+
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.
4+
5+
There are multiple ways to do this, one of which is the standard way of using `[index]` access.
6+
7+
One other way is to use [`charAt`][mdn-char-at], which is the same as `[index]` access for most purposes.
8+
9+
Another method is [`at`][mdn-at], which is the same as `[index]` access, except it accepts negative numbers.
10+
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. These string methods are normally used to get portions of strings, rather than a single character.
14+
15+
An important distiction is that `slice` accepts negative numbers like `at` does, but `substring` does not.
16+
17+
## Different ways to implement `frontDoorPassword`
18+
19+
For `frontDoorPassword`, there are a variety of ways to make strings uppercase and lowercase.
20+
21+
### Approach: `toUpperCase` and `toLowerCase`
22+
23+
```js
24+
export function frontDoorPassword(word) {
25+
return word[0].toUpperCase() + word.slice(1).toLowerCase();
26+
}
27+
```
28+
29+
This approach is a standard method that uses [`toUpperCase`][mdn-to-upper-case] and [`toLowerCase`][mdn-to-lower-case].
30+
31+
### Approach: `toLocaleUpperCase` and `toLocaleLowerCase`
32+
33+
```js
34+
export function frontDoorPassword(word) {
35+
return word[0].toLocaleUpperCase() + word.substring(1).toLocaleLowerCase();
36+
}
37+
```
38+
39+
This approach uses [`toLocaleUpperCase`][mdn-to-locale-upper-case] and [`toLocaleLowerCase`][mdn-to-locale-lower-case], which are very similar to `toUpperCase` and `toLowerCase`.
40+
41+
When using `toLocaleUpperCase` or `toLocaleLowerCase`, you can specify a locale string as an argument and the output will be adjusted to that locale.
42+
43+
### Approach: `String.fromCharCode` and `charCodeAt`
44+
45+
```js
46+
export function frontDoorPassword(word) {
47+
let charCode = word.charCodeAt(0);
48+
if (charCode >= 97) charCode -= 32;
49+
let password = String.fromCharCode(charCode);
50+
for (let index = 1; index < word.length; index++) {
51+
charCode = word.charCodeAt(index);
52+
if (charCode <= 90) charCode += 32;
53+
password += String.fromCharCode(charCode);
54+
};
55+
return password;
56+
}
57+
```
58+
59+
This approach uses [`String.fromCharCode`][mdn-from-char-code] along with [`charCodeAt`][mdn-char-code-at].
60+
61+
This method is much longer than the others and it only works with english letters, so it is less than ideal.
62+
63+
## Different ways to implement `backDoorResponse`
64+
65+
There are many ways to go about trimming whitespace for `backDoorResponse`.
66+
67+
### Approach: `trim` and `[index]` access
68+
69+
```js
70+
export function backDoorResponse(line) {
71+
const trimmed = line.trim();
72+
return trimmed[trimmed.length - 1];
73+
}
74+
```
75+
76+
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.
77+
78+
### Approach: `trimEnd` and `at`
79+
80+
```js
81+
export function backDoorResponse(line) {
82+
return line.trimEnd().at(-1);
83+
}
84+
```
85+
86+
This approach uses the string method [`trimEnd`][mdn-trim-end], which only trims trailing whitespace, unlike `trim`.
87+
88+
It also uses `at` instead of `[index]` access make the solution shorter.
89+
90+
### Approach: `replaceAll` and `charAt`
91+
92+
```js
93+
export function backDoorResponse(line) {
94+
const trimmed = line.replaceAll(' ', '');
95+
return trimmed.charAt(trimmed.length - 1);
96+
}
97+
```
98+
99+
This approach uses [`replaceAll`][mdn-replace-all] to remove all of the spaces in the string.
100+
101+
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.
102+
103+
### Approach: `replace` and literal `RegExp`
104+
105+
```js
106+
export function backDoorResponse(line) {
107+
const trimmed = line.replace(/\s/g, '');
108+
return trimmed[trimmed.length - 1];
109+
}
110+
```
111+
112+
This approach uses [`replace`][mdn-replace] with a [regular expression literal][mdn-regular-expressions], achieving a similar result to the previous approach.
113+
114+
The main difference is that the previous approach only removes spaces, while this approach can remove any type of whitespace.
115+
116+
### Approach: `for` loop
117+
118+
```js
119+
export function backDoorResponse(line) {
120+
for (let index = line.length - 1; index >= 0; index--) {
121+
if (line[index] != ' ') {
122+
return line[index];
123+
};
124+
};
125+
return '';
126+
}
127+
```
128+
129+
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.
130+
131+
[mdn-char-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt
132+
[mdn-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/at
133+
[mdn-substring]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
134+
[mdn-slice]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
135+
[mdn-to-upper-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
136+
[mdn-to-lower-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase
137+
[mdn-to-locale-upper-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase
138+
[mdn-to-locale-lower-case]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleLowerCase
139+
[mdn-from-char-code]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCharCode
140+
[mdn-char-code-at]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
141+
[mdn-trim]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim
142+
[mdn-trim-end]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd
143+
[mdn-replace-all]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
144+
[mdn-replace]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
145+
[mdn-regular-expressions]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions
146+
[mdn-for]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for

0 commit comments

Comments
 (0)