Skip to content
This repository was archived by the owner on Dec 11, 2022. It is now read-only.

Latest commit

 

History

History
101 lines (68 loc) · 2.46 KB

solutions.md

File metadata and controls

101 lines (68 loc) · 2.46 KB

Module 9 Solutions

Back to Module 8

03 - Reduce State Changes

To reduce state changes, you prevent two things from happening:

  1. reassignments
  2. mutations

04 - Don't reassign

  1. You should always declare variables with const. When you declare variables with const, you'll see the const keyword. It is a good indicator that the variable is created in the current scope; it will never change the external state.

  2. If you need a if/else statement to determine what value your variable should take, consider using a ternary operator instead of an if/else statement.

  3. If you need three or more if statements, consider writing a function that uses an early return. This makes your code more declarative as a result.

07 - Preventing objects from mutating

You can prevent objects from mutating through two methods:

Object.assign
assignment
  1. Combine two objects with Object.assign.
const car = {
    type: 'car',
    fueltype: 'gas',
    wheels: 4
}

const teslaOptions = {
    color: 'black',
    fueltype: 'electric'
}

const createVehicle = (baseObj, objOptions) => Object.assign({}, baseObj, objOptions)

const newTesla = createVehicle(car, teslaOptions)

console.log(newTesla)
// => color: "black", fueltype: "electric", type: "car", wheels: 4
  1. Combine two objects with assignment.
const car =  {
    vehicleType: {
        car: true
        class: 'c'
    },
    fueltype: 'gas',
    wheels: 4
}

const teslaOptions = {
    color: 'black',
    fueltype: 'electric',
    package: {
        trim: 'S',
        line: '95'
    }
}

const createVehicle = (baseObj, objOptions) => assignment({}, baseObj, objOptions)

const newTesla = createVehicle(car, teslaOptions)

08 - Preventing arrays from mutating

Array.slice - you tell JavaScript which parts of the array you want to copy out as your new array.

const newArray = array.slice(startIndex, endIndex)

// omit indexes to get the entire array
const copyArray = array.slice();

Array.concat - combines 2 arrays to form a new one.

Back to top

On to Module 10