📝
marcyannotes
  • Welcome
  • Student Guidelines & Policies
    • Student Handbook
    • AI Policy
    • Academic Calendar
  • Environment Setup
    • Local Environment Setup - Mac
    • Local Environment Setup - Windows
    • GitHub Setup
    • Postgres Setup
  • Fullstack Software Engineering Curriculum
    • Overview
    • How-Tos
      • How To Code at Marcy: Code Style Guide
      • How to Do Short Response and Coding Assignments
      • How to Debug
      • How to PEDAC
      • How to Create Projects with Vite
      • How to Deploy on GitHub Pages
      • How to Deploy on Render
    • Mod 0 - Command Line Interfaces, Git, and GitHub
      • Mod 0 Overview
      • Command Line Interfaces
      • Git & GitHub
      • Git Pulling & Merging
      • Git Branching & PRs
      • Pair Programming: BONUS
    • Mod 1 - JavaScriptFundamentals
      • Mod 1 Overview
      • Intro to Programming
      • Errors
      • Node & Node Modules
      • Variables, Functions & String Methods
      • Control Flow, typeof, and Math
      • Loops
      • Arrays
      • Objects
      • Higher Order Functions: Callbacks
      • Higher Order Functions: Array Methods
      • Regex
    • Mod 2 - HTML, CSS & the DOM
      • Mod 2 Overview
      • HTML
      • CSS
      • Accessibility (a11y)
      • The DOM
      • Events
      • Forms
      • The Box Model and Positioning
      • Flexbox
      • Grid & Media Queries
      • ESModules
      • LocalStorage
    • Mod 3 - Async & APIs
      • Mod 3 Overview
      • Promises
      • Fetch
      • Building a Fetching App
      • Async & Await
    • Mod 4 - Project Week!
      • Project Week Overview
    • Mod 5 - Object-Oriented Programming
      • Mod 5 Overview
      • Intro to OOP, Encapsulation, Factory Functions, and Closure
      • Classes
      • Private & Static
      • Has Many/Belongs To
      • Polymorphism
    • Mod 6 - Data Structures & Algorithms
      • Mod 6 Overview
      • Stacks & Queues
      • Nodes & Linked Lists
      • Singly & Doubly Linked Lists
      • Recursion
      • Trees
    • Mod 7 - React
      • Mod 7 Overview
      • Intro to React
      • Events, State, and Forms
      • Fetching with useEffect
      • Building a Flashcards App
      • React Context
      • Global Context Pattern
      • React Router
    • Mod 8 - Backend
      • Mod 8 Overview
      • Intro to Express
      • Building a Static Web Server with Middleware
      • Securing API Keys with Environment Variables
      • Building a RESTful API with MVC
      • SQL and Databases
      • JOIN (Association) SQL Queries
      • Knex
      • Your First Fullstack App!
      • Migrations & Seeds
      • Schema Design & Normalization
      • Hashing Passwords with Bcrypt
  • Code Challenge Curriculum
    • Unit 0
      • Lecture: Functions in JS
      • CC-00: Functions and Console Logs
      • CC-01: Conditionals
      • CC-02: Conditionals 2
    • Unit 1
      • CC-03: For Loops
      • CC-04: For Loops and Conditionals
      • CC-05: For Loops and Conditionals 2
    • Unit 2
      • CC-06: String Mutations
      • CC-07: Array Iteration
      • CC-08: String Mutation and Array Iteration
      • CC-09: Array Mutations
      • CC-10: Reading Objects
      • CC-11: Objects
      • CC-12: Objects
      • Unit 2 Diagnostic
    • Unit 3
      • Intro to PEDAC (and Algorithms)
      • validTime
      • fizzBuzz (array)
      • digitSumDifference
      • firstNotRepeating
      • compareEvenAndOddSum
      • countVowelConsonants
      • finalHP
      • canMakeTriangle
    • Unit 4
    • Unit 5
    • Unit 6
    • Unit 7
    • Unit 8
    • Sorting
Powered by GitBook
On this page
  • Strings
  • Concatenation
  • Carmen's Code Snippets
  • Code Challenge
  1. Code Challenge Curriculum
  2. Unit 2

CC-06: String Mutations

PreviousUnit 2NextCC-07: Array Iteration

Last updated 7 months ago

with Carmen Salas • 2024/10/10

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

Although what JS really does is just make a new resulting string, since strings are immutable.

  • using the +/+= operator (can be between a str<->str, str<->num, etc.)

  • using String methods like .concat(), .padEnd(), .repeat()

Carmen's Code Snippets

//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?

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(""))       //""
  1. 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.

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"
slides
Strings
Concatenation
Carmen's Code Snippets
Code Challenge