📝
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
  • Truthys and Falsys
  • typeof Hierarchy in JS:
  • Conditionals Overview
  • Comparison Operators
  • Code Challenge
  1. Code Challenge Curriculum
  2. Unit 0

CC-01: Conditionals

PreviousCC-00: Functions and Console LogsNextCC-02: Conditionals 2

Last updated 7 months ago

with Carmen Salas • 2024/10/02


Truthys and Falsys

Truthy JS values:

  • '0' - a string containing a single zero

  • 'false' - a string containing 'false'

  • [] - empty array

  • {} - empty object

  • function(){} - an empty function

Falsy JS values:

  • false

  • 0 zero as a number and -0 minus zero as a number

  • '', "" - empty strings

  • null

  • undefined

  • NaN

Important tidbit:

// these are the only ways for `null` and `undefined` to be truthy:
null == null
null == undefined
undefined == undefined
undefined == null

The only way for null and undefined to be true when compared with equality operators is if it is compared to each other (or itself).

typeof Hierarchy in JS:

.
├── boolean
├── number
│   ├── NaN
│   └── number
├── object
│   ├── Array
│   ├── null
│   └── object
├── string
└── undefined

This is not from any official doc, but something I tested when working with the swe-1-3 homework!

Conditionals Overview

if () {} else if () {} else {}

Comparison Operators

The strict equality operator === always considers operands of different types to be different.

  • not all languages implement this operator!

'1' == 1
'1' === 1

Test this out in node!

Code Challenge

These solutions implement the refactored version, and is NOT the neatest way to write them (sorry!).

  1. Write a function named greatestOfTwo that takes in two integer arguments and returns the largest of both arguments. You can assume both integers will be different values.

const greatestOfTwo = (a,b) => {
  if (a == b) {
    if (a == undefined) {
      return 'No arguments were passed';
    }
  } else if (a == undefined) {
      return 'Only one argument was passed';
  } else if (b == undefined) {
      return 'Only one argument was passed';
  } else {
    return (a > b) ? a : b;
  }
}

// tests
console.log(greatestOfTwo(14,1)); // returns 14
console.log(greatestOfTwo(-2,2)); // returns 2
console.log(greatestOfTwo()); // "No arguments were passed"
console.log(greatestOfTwo(-2)); // "Only one argument was passed"
  1. Write a function named areBothSame, that takes in two arguments and returns true if both arguments are truthy in value, and returns false if not.

const areBothSame = (a,b) => {
  if (a == b) {
    if (a == undefined) {
      return 'No arguments were passed';
    }
    return true;
  } else if (a == undefined) {
      return 'Only one argument was passed';
  } else if (b == undefined) {
      return 'Only one argument was passed';
  } else {
    return false;
  }
}

// tests
console.log(areBothSame(true,1)); // returns true
console.log(areBothSame("2",2)); // returns true
console.log(areBothSame("5",2)); // returns false
console.log(areBothSame(1)); // "Only one argument was passed"
console.log(areBothSame()); // "No arguments were passed"
  1. Write a function named areBothEqual, that takes in two arguments and returns true if both arguments are the same in data type and value, and returns false if they are not.

const areBothEqual = (a,b) => {
  if (a === b) {
    if (a === undefined) {
      return 'No arguments were passed';
    }
    return true;
  } else if (a == undefined) {
      return 'Only one argument was passed';
  } else if (b == undefined) {
      return 'Only one argument was passed';
  } else {
    return false;
  }
}

// test
console.log(areBothEqual("2", "2")); // returns true
console.log(areBothEqual("2", 2)); // returns false
console.log(areBothEqual(true, 1)); // returns false
console.log(areBothEqual(7, 7)); // returns true
console.log(areBothEqual(7)); // "Only one argument was passed"
console.log(areBothEqual()); // "No arguments were passed"
slides
Truthys and Falsys
Type Hierarchy in JS
Conditionals Overview
Comparison Operators
Code Challenge