📝
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
  1. Code Challenge Curriculum
  2. Unit 3

validTime

with Carmen Salas • 2024/10/29

Code Challenge

/*
## PEDAC
**Problem**
input: string representing time in the format: HH:MM
output: boolean representing if the string representation is correct or otherwise
task: validate the time string

**Examples/Edge Case:** 
22:61 -> false
23:23 -> true

**Data Structure**
- Strings
- Arrays
- Numbers
- Conditional Statements to validate
- (maybe) regex

**Algorithm** 
WOW THESE MAY BE SKIPPABLE TOO LMAO READING
1. Find a colon in the string (if not present, INVALIDATE)
2. Check if length of string > 5 (if yes, INVALIDATE)

3. Split string into array based on ":" as separator.

ME WHEN I DONT READ STEP 4 IS SKIPPABLE LOL
4. Convert both strings into Numbers. (if either side can't be converted, INVALIDATE)
    4.a Check if left side is between 00-23 (otherwise, INVALIDATE)
    4.b Check if right side is between 00-59 (otherwise, INVALIDATE)
    
5. Return true (since all cases should already be covered)

NEW ALGORITHM BECAUSE I DONT READ
1. Split string into array based on ":" as separator
2. Check if left side is between 00-23 (otherwise, INVALIDATE)
3. Check if right side is between 00-59 (otherwise, INVALIDATE)
4. Return true (since all cases should already be covered)


**Code** 
*/

function solution(time) {
    // // WOW THESE MAY BE SKIPPABLE TOO LMAO READING
    // if (!time.includes(":")) { return false; } // step #1
    // if (time.length > 5) { return false; } // step #2
    // const timeArr = time.split(":") // step #3
    
    // // for clarity we can name the values
    // const hours = Number(timeArr[0]);
    // const minutes = Number(timeArr[1]);
    
    // // in hindsight, will this return an error anyway?
    // // how do u stop it if input is actually NaN?
    // // NVM it works!
    // // step #4
    // // ME WHEN I DONT READ STEP 4 IS SKIPPABLE LOL
    // if (isNaN(hours)) { return false; }
    // if (isNaN(minutes)) { return false; }
    
    // if(hours < 0 || hours > 23) { return false; } // step #4a
    // if(minutes < 0 || minutes > 59) { return false; } // step #4b
    
    // return true; // step #5
    
    // NEW ALGO WITH READING COMPREHENSION INTO ACCOUNT
    // Step #1
    const timeArr = time.split(":");
    // Name the sides for ease of use
    const hours = Number(timeArr[0]);
    const minutes = Number(timeArr[1]);
    // Step #2
    if (hours < 0 || hours > 23) { return false; }
    // Step #3
    if (minutes < 0 || minutes > 59) { return false; }
    // Step #4
    return true;
}
PreviousIntro to PEDAC (and Algorithms)NextfizzBuzz (array)

Last updated 7 months ago