📝
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 2

Unit 2 Diagnostic

with Carmen Salas • 2024/10/24

Questions

  1. Write a function named sum that takes in a string of digits, the function should return the sum of each digit in the string. You can assume there will be no spaces in the given string, you should return an integer.

const sum = (str) => {
  return str.split('').reduce((sum,char) => Number(sum) + Number(char));
}

console.log(sum("123")); // returns 6, because 1+2+3 = 6
console.log(sum("2501")); //returns 8, because 2+5+0+1 = 8
  1. Write a function isNumWithinRange that takes in three integer arguments, a number, a min number, and a max number. The function should return a boolean based on if the first argument is between the range of the min and max number.

const isNumWithinRange = (num, min, max) => {
  return (num <= max && num >= min);
}

console.log(isNumWithinRange(3, 1, 5)); //returns true, because 3 is between 1 and 5 
console.log(isNumWithinRange(5, 1, 5));//returns true, because 5 is between 1 and 5 
console.log(isNumWithinRange(11, 5, 10)); //returns false, because 11 is not between 5 and 
  1. Write a function named countFromOne that takes in an integer argument, and returns an array of integers, where the elements are the numbers between 1 and the given argument, ordered from least to greatest.

const countFromOne = (int) => {
  return [...Array(int)].map((val, index, arr) => index+1);
}

console.log(countFromOne(5)) //returns [1,2,3,4,5]
console.log(countFromOne(0)) //returns []
console.log(countFromOne(10)) //returns [1,2,3,4,5,6,7,8,9,10]
  1. Declare a function named findShortestWord, that takes an array of strings as an argument, and returns the shortest string in the array. If there is more than one shortest string, the function should return the first shortest string that appears in the array.

const findShortestWord = (strArr) => {
  return strArr.reduce((res, str) => (str.length < res.length) ? str : res);
}

console.log(findShortestWord(["The","quick","brown", "fox", "jumped", "over", "the", "lazy", "dog"])) //returns "The"
console.log(findShortestWord(["jazzy", "jumpy", "quaky"])) //returns "jazzy"
  1. Write a function named vowelCount that takes in a string and returns an object where the keys are letters in the given string that are vowels and the value of each vowel key is a count of how many times the character appears in the string. For this problem, vowels are a,e,i,o,u.

const vowelCount = (str) => {
  return str.match(/[aeiou]/g).reduce((res, char) => {
    (res[char]) ? res[char]++ : res[char] = 1;
    return res;
  }, {});
}

console.log(vowelCount("aaa")) // returns {"a": 3}
console.log(vowelCount("code challenge")) // returns {"o": 1, "e":3, "a":1,}
PreviousCC-12: ObjectsNextUnit 3

Last updated 7 months ago