This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
1. Use the constructor pattern for object creation using the new keyword. | |
2. Each instance of the object will have its own reference to this. | |
3. Link functions to the object using the prototype. This provides a | |
performance gain because functions aren't copied into each | |
instance of the object. They are linked to the prototype. | |
It's better to have one copy of functions around, not multiple copies. | |
Now, if we can use ES 2015 syntax: | |
1. Use the class keyword. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Draw a spiral matrix. | |
[ 1, 2, 3, 4] | |
[12, 13, 14, 5] | |
[11, 16, 15, 6] | |
[10, 9, 8, 7] | |
1. Initialize the multidimensional array. | |
2. Create counter variables to track positions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Create a function to return the total number of vowels in a string. | |
Use String.prototype.match() to find matches | |
using a regular expression. | |
/[aeiou]/gi | |
// denotes a regex pattern. | |
[] instructs match() to find any of these characters. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This function builds a pyramid using ES6 repeat function available on the String prototype. | |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat | |
colSize: is the width of the grid in columns. Also think of it as the width of the last row in the pyramid. | |
level: This represents the width of the pyramid level at each iteration. | |
numSpaces: This represents the number of spaces to use on both sides of the pyramid. | |
spaces: This variable is used to cache the result of String#repeat(), no need to do the same work twice. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
A simple iterative solution to building steps. | |
Uses String.prototype.repeat which is an ES2015 feature. | |
*/ | |
function steps(n) { | |
for (let i = 1; i <= n; i++) { | |
let step = '#'.repeat(i) + ' '.repeat(n - i); | |
console.log(step); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Create a function that accepts a string, converts the string to title case, and | |
returns the result. For title case, the first letter of each word is capitalized. | |
capitalize('hello world'); //Outputs: 'Hello World' | |
captialize('the year of the hare'); //Outputs: 'The Year Of The Hare'; | |
Pseudo code: | |
1. Split the string into array using str.split(' '); | |
2. Map over the elements in the array. Title case each word using helper function. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Check to see if two strings are anagrams, where both strings have the same characters in the same quantity. | |
Only consider characters, not spaces and punctuation. Consider the strings as case insensitive, where capital | |
letters are the same as lowercase letters. | |
*/ | |
function anagram(str1, str2) { | |
//Step 1: Create a data store for each string. | |
const charMap1 = getCharMap(str1); | |
const charMap2 = getCharMap(str2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given an array and size, create a new array containing chunked elements with the size provided. | |
For example, given the array ['Alligator', 'Bear', 'Cat', 'Dog', 'Elephant', 'Flamingo', 'Giraffe'] and size 2, | |
return an array of arrays with 2 elements per array. | |
Result: | |
[["Alligator", "Black Bear"], ["Cat", "Dog"], ["Elephant", "Flamingo"], ["Giraffe"]] | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Create a function that console logs the numbers from 1 to n. | |
- Print "fizz" for multiples of three instead of the number. | |
- Print "buzz" for multiples of five instead of the number. | |
- Print "fizzbuzz" for numbers that are multiples of three and five instead of the number. | |
- Otherwise, print the number itself. | |
*/ | |
function fizzBuzz(num) { | |
for (let i = 1; i <= num; i++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a string, find the character that has the highest frequency. | |
*/ | |
function maxChar(str) { | |
//Step 1: Create a data store. | |
let ds = {}; | |
//Step 2: Populate it. | |
for (let char of str) { |