# CC-06: String Mutations

with Carmen Salas • 2024/10/10

[slides](https://docs.google.com/presentation/d/1oRtJvixVl1Ihwcv4mrk3G2__CcIvhgQYzq1Rg9__Vt8/edit#slide=id.g287c4ecafef_0_150)

* [Strings](#strings)
* [Concatenation](#concatenation)
* [Carmen's Code Snippets](#carmens-code-snippets)
* [Code Challenge](#code-challenge)

## Strings

Strings are made up of `chars`. Every `char` in a string has a corresponding position `index`, allowing direct access to them. As a result, strings are **iterable**, and indices start at `0`

Some string properties/methods:

* `.length` returns the size of the `string`
* `.toUpperCase()` and `.toLowerCase()` works for both `char` and `string`. This is because `char` in JavaScript is also a `string`.

## Concatenation

Various ways to *add* `strings`

{% hint style="info" %}
Although what JS really does is just make a new resulting `string`, since `strings` are **immutable**.
{% endhint %}

* using the `+`/`+=` operator (can be between a `str`<->`str`, `str`<->`num`, etc.)
* using `String` methods like `.concat()`, `.padEnd()`, `.repeat()`

## Carmen's Code Snippets

```js
//Review of strings
/*
- Strings are made up of characters.

- Every character in a string has a position, this position is called the index. You can access any charcater in a string if you know it's position

- This means that strings are iterable. 
- Zero based indices 

Some string properties/methods:
- "hello".length  === 5
   01234
   Bracket notation: "hello"[2] //"l"
- .toUpperCase() / .toLowerCase()

Concatenation:
"hello" + "o" -> "helloo"

*/
let str = "hello";

for (let i = 0; i <= str.length - 1; i++) {
  console.log(i);
  console.log(typeof str[i]);
}

// let empty = "";
// empty += "h";
// console.log(empty);
// empty += "i";
// console.log(empty);
```

## Code Challenge

1. Declare a function named `reverseString` that takes in a string argument and returns the reversed version of the string, without using any `.reverse()` JavaScript methods. **Hint:** How can you use a loop iterate through a string backwards?

```js
const reverseString = (str) => {
  res = "";

  for (let i = str.length-1; i >= 0; i--) {
    res += str[i];
  }

  return res
}

// console.log(reverseString("hello"))  //"olleh"
// console.log(reverseString("oo"))     //"oo"
// console.log(reverseString(""))       //""
```

2. Declare a function named `reverseZigZagString` that takes in a string argument and returns the reversed version of the string with characters alternating in uppercase and lowercase, without using any `.reverse()` JavaScript methods.

```js
const reverseZigZagString = (str) => {
  // setup:
  // `res` to build up
  // `small` to alternate properly when string length is even
  res = "";
  let small = true;
  for (let i = str.length-1; i >= 0; i--) {
    // account for spaces
    if (str[i] == ' ') {
      res += str[i];
      continue;
    }
    // alternate chars
    (small) ? res += str[i].toLowerCase() : res += str[i].toUpperCase();
    small = !small;
  }

  return res;
}

// console.log(reverseZigZagString("hello")) //"oOlLeH"
// console.log(reverseZigZagString("Fellows"))    //"sWoLlEf"
// console.log(reverseZigZagString("Code Challenge"))  //"eGnElLaHc EdOc"
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://raffycastlee.gitbook.io/marcyannotes/codechallenge-curriculum/unit-2/20241010.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
