◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
14 changes: 7 additions & 7 deletions 1-js/05-data-types/03-string/1-ucfirst/solution.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
We can't "replace" the first character, because strings in JavaScript are immutable.

But we can make a new string based on the existing one, with the uppercased first character:

```js
let newStr = str[0].toUpperCase() + str.slice(1);
```

There's a small problem though. If `str` is empty, then `str[0]` is `undefined`, and as `undefined` doesn't have the `toUpperCase()` method, we'll get an error.

There are two variants here:

1. Use `str.charAt(0)`, as it always returns a string (maybe empty).
2. Add a test for an empty string.

Here's the 2nd variant:

```js run demo
function ucFirst(str) {
Expand Down
4 changes: 2 additions & 2 deletions 1-js/05-data-types/03-string/1-ucfirst/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# Uppercase the first character

Write a function `ucFirst(str)` that returns the string `str` with the uppercased first character, for instance:

```js
ucFirst("john") == "John";
Expand Down
2 changes: 1 addition & 1 deletion 1-js/05-data-types/03-string/2-check-spam/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
To make the search case-insensitive, let's bring the string to lower case and then search:

```js run demo
function checkSpam(str) {
Expand Down
4 changes: 2 additions & 2 deletions 1-js/05-data-types/03-string/3-truncate/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The maximal length must be `maxlength`, so we need to cut it a little shorter, to give space for the ellipsis.

Note that there is actually a single Unicode character for an ellipsis. That's not three dots.

```js run
function truncate(str, maxlength) {
8 changes: 4 additions & 4 deletions 1-js/05-data-types/03-string/3-truncate/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 5

---

# Truncate the text

Create a function `truncate(str, maxlength)` that checks the length of the `str` and, if it exceeds `maxlength` -- replaces the end of `str` with the ellipsis character `"…"`, to make its length equal to `maxlength`.

The result of the function should be the truncated (if needed) string.

For instance:

```js
truncate("What I'd like to tell on this topic is:", 20) = "What I'd like to te…"
Expand Down
9 changes: 4 additions & 5 deletions 1-js/05-data-types/03-string/4-extract-currency/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ importance: 4

---

# Extract the money

We have a cost in the form `"$120"`. That is: the dollar sign goes first, and then the number.

Create a function `extractCurrencyValue(str)` that would extract the numeric value from such string and return it.

The example:

```js
alert( extractCurrencyValue('$120') === 120 ); // true
```

Loading
Toggle all file notes Toggle all file annotations