You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercises/concept/poetry-club-door-policy/.approaches/introduction.md
+28-8Lines changed: 28 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,17 @@
1
1
# Introduction
2
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.
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.
4
5
5
6
There are multiple ways to do this, one of which is the standard way of using `[index]` access.
6
7
7
8
One other way is to use [`charAt`][mdn-char-at], which is the same as `[index]` access for most purposes.
8
9
9
10
Another method is [`at`][mdn-at], which is the same as `[index]` access, except it accepts negative numbers.
10
-
11
11
A negative number will count backwards from the end of the string, unlike positive numbers, which count forwards from the start.
12
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.
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.
14
15
15
16
An important distiction is that `slice` accepts negative numbers like `at` does, but `substring` does not.
16
17
@@ -36,20 +37,38 @@ export function frontDoorPassword(word) {
36
37
}
37
38
```
38
39
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
+
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
+
conststr="istanbul";
45
+
46
+
str.toUpperCase()
47
+
// => 'ISTANBUL'
48
+
str.toLocaleUpperCase('en-US')
49
+
// => 'ISTANBUL'
40
50
41
-
When using `toLocaleUpperCase` or `toLocaleLowerCase`, you can specify a locale string as an argument and the output will be adjusted to that locale.
51
+
str.toLocaleUpperCase('tr')
52
+
// => 'İSTANBUL'
53
+
```
42
54
43
55
### Approach: `String.fromCharCode` and `charCodeAt`
44
56
45
57
```js
46
58
exportfunctionfrontDoorPassword(word) {
47
59
let charCode =word.charCodeAt(0);
48
-
if (charCode >=97) charCode -=32;
60
+
if (charCode >=97) {
61
+
charCode -=32;
62
+
}
63
+
49
64
let password =String.fromCharCode(charCode);
65
+
50
66
for (let index =1; index <word.length; index++) {
51
67
charCode =word.charCodeAt(index);
52
-
if (charCode <=90) charCode +=32;
68
+
if (charCode <=90) {
69
+
charCode +=32;
70
+
}
71
+
53
72
password +=String.fromCharCode(charCode);
54
73
}
55
74
return password;
@@ -126,7 +145,8 @@ export function backDoorResponse(line) {
126
145
}
127
146
```
128
147
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.
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.
0 commit comments