Skip to content

Commit

Permalink
Merge pull request #139 from LaunchCodeEducation/Ticket-35-broken-lin…
Browse files Browse the repository at this point in the history
…ks-in-common-string-reading

Ticket 35 broken links in 'Common String Methods' table
  • Loading branch information
mlambert125 authored Sep 6, 2024
2 parents 618b39f + b4c3130 commit ed583b4
Show file tree
Hide file tree
Showing 11 changed files with 360 additions and 9 deletions.
2 changes: 1 addition & 1 deletion content/appendices/array-method-examples/_index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Array Method Examples"
date: 2021-10-01T09:28:27-05:00
weight: 7
weight: 2
draft: false
originalAuthor: John Woolbright # to be set by page creator
originalAuthorGitHub: jwoolbright23 # to be set by page creator
Expand Down
2 changes: 1 addition & 1 deletion content/appendices/dom-method-examples/_index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "DOM Method Examples"
date: 2021-10-01T09:28:27-05:00
weight: 8
weight: 4
draft: false
originalAuthor: John Woolbright # to be set by page creator
originalAuthorGitHub: jwoolbright23 # to be set by page creator
Expand Down
2 changes: 1 addition & 1 deletion content/appendices/math-method-examples/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "Math Method Examples"
date: 2021-10-01T09:28:27-05:00
draft: false
weight: 10
weight: 3
originalAuthor: Sally Steuterman # to be set by page creator
originalAuthorGitHub: gildedgardenia # to be set by page creator
reviewer: # update any time edits are made after review
Expand Down
15 changes: 15 additions & 0 deletions content/appendices/string-method-examples/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: "String Method Examples"
date: 2024-09-05T16:28:27-05:00
draft: false
weight: 1
originalAuthor: Christian Frauenhoffer # to be set by page creator
originalAuthorGitHub: johncfrauen-lc101 # to be set by page creator
reviewer: # update any time edits are made after review
reviewerGitHub: # update any time edits are made after review
lastEditor: # update any time edits are made after review
lastEditorGitHub: # update any time edits are made after review
lastMod: # UPDATE ANY TIME CHANGES ARE MADE
---

{{% children %}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
title: "indexOf Examples"
date: 2024-09-05T16:28:27-05:00
draft: false
weight: 1
originalAuthor: Christian Frauenhoffer # to be set by page creator
originalAuthorGitHub: johncfrauen-lc101 # to be set by page creator
reviewer: # update any time edits are made after review
reviewerGitHub: # update any time edits are made after review
lastEditor: # update any time edits are made after review
lastEditorGitHub: # update any time edits are made after review
lastMod: # UPDATE ANY TIME CHANGES ARE MADE
---

The general syntax for this method is:

```console
stringName.indexOf(substr);
```

Given a candidate substring, this method returns the integer index of the *first* occurrence of the substring in the string. If the substring does not occur in the string, -1 is returned.

{{% notice blue "Example" "rocket" %}}

```js {linenos=table}
console.log("LaunchCode".indexOf("C"));

console.log("LaunchCode".indexOf("A"));

console.log("dogs and dogs and dogs!".indexOf("dog"));
```

**Console Output**

```console
6
-1
0
```

{{% /notice %}}

{{% notice blue "Example" "rocket" %}}
An email address must contain an `@` symbol. Checking for the presence of this symbol is a part of email address verification in most programs.

```js {linenos=table}
let input = "[email protected]";
let atIndex = input.indexOf("@");

if (atIndex > -1) {
console.log("Email contains @");
} else {
console.log("Invalid email");
}
```

**Console Output**

```console
Email contains @
```

{{% /notice %}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
title: "replace Examples"
date: 2024-09-05T16:28:27-05:00
draft: false
weight: 5
originalAuthor: Christian Frauenhoffer # to be set by page creator
originalAuthorGitHub: johncfrauen-lc101 # to be set by page creator
reviewer: # update any time edits are made after review
reviewerGitHub: # update any time edits are made after review
lastEditor: # update any time edits are made after review
lastEditorGitHub: # update any time edits are made after review
lastMod: # UPDATE ANY TIME CHANGES ARE MADE
---

The general syntax for this method is:

```console
stringName.replace(searchChar, replacementChar);
```

Given a search string `searchChar` and a replacement value `replacementChar`, this method returns a copy of `stringName` with the *first* occurrence of `searchChar` replaced by `replacementChar`.

{{% notice blue Note "rocket" %}}
The `replace` method can be used in more powerful ways utilizing regular expressions. We will not cover those here, but you [can read more at MDN.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)
{{% /notice %}}


{{% notice blue "Example" "rocket" %}}
```js {linenos=table}

console.log("carrot".replace("r", "t"));

console.log("Launch Code".replace(" ", ""));
```
**Console Output**

```console
catrot
LaunchCode
```
{{% /notice %}}

{{% notice blue "Example" "rocket" %}}
Some email providers, including Gmail, allow users to put a `.` anywhere before the `@` symbol. This means that `[email protected]` is the same as `[email protected]`.

Remove the `.` before the `@` symbol in an email address.

```js {linenos=table}
let input = "[email protected]";
let email = input.replace(".", "");
console.log(email);
```
**Console Output**

```console
[email protected]
```

This example illustrates a common use case of `replace`, which is to remove a character by replacing it with an empty string.

{{% /notice %}}

{{% notice orange "Warning" "rocket" %}}

Notice in the last example that if there is not a `.` before the `@` symbol, the `.` that is part of the domain, `launchcode.org` would be inadvertently removed. In a real application, we would want to isolate the portion in front of `@` using `slice`.

{{% /notice %}}
54 changes: 54 additions & 0 deletions content/appendices/string-method-examples/slice-examples/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
title: "slice Examples"
date: 2024-09-05T16:28:27-05:00
draft: false
weight: 6
originalAuthor: Christian Frauenhoffer # to be set by page creator
originalAuthorGitHub: johncfrauen-lc101 # to be set by page creator
reviewer: # update any time edits are made after review
reviewerGitHub: # update any time edits are made after review
lastEditor: # update any time edits are made after review
lastEditorGitHub: # update any time edits are made after review
lastMod: # UPDATE ANY TIME CHANGES ARE MADE
---


The general syntax for this method is:

```console
stringName.slice(i, j);
```

Given a starting index `i` and an optional ending index `j`, return the substring consisting of characters from index `i` through index `j-1`. If the ending index is ommitted, the returned substring includes all characters from the starting index through the end of the string.

```js {linenos=table}
"LaunchCode".slice(0, 6);

"LaunchCode".slice(6);
```

**Console Output**

```console
Launch
Code
```

On some websites, the portion of an email address before the `@` symbol is used as a username. We can extract this portion of an email address using `slice` in conjunction with `indexOf`.


{{% notice blue "Example" "rocket" %}}
```js {linenos=table}
let input = "[email protected]";
let atIndex = input.indexOf("@");
let username = input.slice(0, atIndex);
console.log(username);
```

**Console Output**

```console
fake.email
```

{{% /notice %}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: "toLowerCase Examples"
date: 2024-09-05T16:28:27-05:00
draft: false
weight: 2
originalAuthor: Christian Frauenhoffer # to be set by page creator
originalAuthorGitHub: johncfrauen-lc101 # to be set by page creator
reviewer: # update any time edits are made after review
reviewerGitHub: # update any time edits are made after review
lastEditor: # update any time edits are made after review
lastEditorGitHub: # update any time edits are made after review
lastMod: # UPDATE ANY TIME CHANGES ARE MADE
---

The general syntax for this method is:

```console
stringName.toLowerCase();
```

This method returns a copy of `stringName` with all uppercase letters replaced by their lowercase counterparts. It leaves non-alphabetic characters unchanged.

```js
"LaunchCode".toLowerCase();
```

**Console Output**

```console
launchcode
```

{{% notice blue "Example" "rocket" %}}
The domain portion of an email address (the portion after the `@` symbol) is case-insensitive. Emails with domain `launchcode.org` are the same as those with domain `LAUNCHCODE.ORG`. By convention, the all-lowercase version is typically used by an application.

This program standardizes an email address by converting it to all lowercase characters.

```js {linenos=table}
let input = "[email protected]";

let email = input.toLowerCase();

console.log(email);
```

**Console Output**

```console
[email protected]
```

{{% /notice %}}

{{% notice orange "Warning" "rocket" %}}
This example is a bit crude, since the portion of an email address before the `@` symbol is allowed to be case-sensitive. When standardizing the case of an email in a real application, we would want to be more precise and only convert the domain portion to lowercase characters.

{{% /notice %}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: "toUpperCase Examples"
date: 2024-09-05T16:28:27-05:00
draft: false
weight: 3
originalAuthor: Christian Frauenhoffer # to be set by page creator
originalAuthorGitHub: johncfrauen-lc101 # to be set by page creator
reviewer: # update any time edits are made after review
reviewerGitHub: # update any time edits are made after review
lastEditor: # update any time edits are made after review
lastEditorGitHub: # update any time edits are made after review
lastMod: # UPDATE ANY TIME CHANGES ARE MADE
---

The general syntax for this method is:

```console
stringName.toUpperCase();
```

This method returns a copy of `stringName` with all lowercase letters replaced by their uppercase counterparts. It leaves non-alphabetic characters unchanged.

{{% notice blue "Example" "rocket" %}}

```js {linenos=table}
console.log("LaunchCode".toUpperCase());
console.log("launchcode".toUpperCase());
console.log("LaunchCode's LC101".toUpperCase());
```

**Console Output**

```console
LAUNCHCODE
LAUNCHCODE
LAUNCHCODE'S LC101
```
{{% /notice %}}
57 changes: 57 additions & 0 deletions content/appendices/string-method-examples/trim-examples/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: "trim Examples"
date: 2024-09-05T16:28:27-05:00
draft: false
weight: 4
originalAuthor: Christian Frauenhoffer # to be set by page creator
originalAuthorGitHub: johncfrauen-lc101 # to be set by page creator
reviewer: # update any time edits are made after review
reviewerGitHub: # update any time edits are made after review
lastEditor: # update any time edits are made after review
lastEditorGitHub: # update any time edits are made after review
lastMod: # UPDATE ANY TIME CHANGES ARE MADE
---


The general syntax for this method is:

```console
stringName.trim();
```

This method returns a copy of the string with any leading or trailing whitespace removed. Whitespace characters are those that do not display anything on the screen, such as spaces and tabs.

{{% notice blue "Example" "rocket" %}}

```js {linenos=table}
console.log("Saint Louis ".trim());
console.log(" Saint Louis".trim());
console.log(" Saint Louis ".trim());
```

**Console Output**

```console
Saint Louis
Saint Louis
Saint Louis
```
{{% /notice %}}

{{% notice blue "Example" "rocket" %}}
When typing an email address into a web site, a user may inadvertently type a space before and/or after the email address. We can clean up such input using the `trim` method.

This example cleans up user input with `trim`.

```js {linenos=table}
let input = " [email protected] ";
let email = input.trim();
console.log(email);
```

**Console Output**

```console
[email protected]
```
{{% /notice %}}
Loading

0 comments on commit ed583b4

Please sign in to comment.