Instantly share code, notes, and snippets.

# GreggGreggSetzer

• Sort options
Last active Feb 3, 2018
Javascript Interview Question: Reverse a string
View reverse-string.js
 /* A few ways to reverse a string using JavaScript. */ //Most direct solution function reverse(str) { return str.split('').reverse().join(''); } //Example using the Array.reduce helper.
Last active Feb 3, 2018
Javascript Interview Question: Palindrome
View palindrome.js
 /* Create a function that checks if a string is a palindrome. */ //Most direct solution function palindrome(str) { return str === str.split('').reverse().join(''); } //Solution that uses Array.every() helper. Note, this is inefficient as you do twice the work.
Last active Feb 3, 2018
Javascript Interview Question: Reverse a Number
View reverse-number.js
 /* Reverse a number. */ function reverseInt(num) { //Convert the num to a string, reverse the chars. const reversedStr = num.toString().split('').reverse().join(''); //Convert back to an int, restore the sign (+ or -), return result as number. return parseInt(reversedStr) * Math.sign(num);
Last active Feb 3, 2018
Javascript Interview Question: Given a string, what character appears the most?
View max-char.js
 /* 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) {
Created Jan 30, 2018
Javascript Interview Question: Fizz Buzz
View fizz-buzz.js
 /* 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++) {
Created Jan 30, 2018
Javascript Interview Question: Chunked Array - Given an array, create a new array containing chunked arrays of the given size.
View chunked-array.js
 /* 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"]] */
Last active Feb 3, 2018
Javascript Interview Question: Anagram
View anagram.js
 /* 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);
Last active Feb 3, 2018
Javascript Interview Question: Title Case - Capitalize the first letter in each word of a string.
View capitalize.js
 /* 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.
Last active Mar 2, 2018
Javascript Interview Question: Constructor Pattern
View constructor-pattern.js
 /* 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.
Created Mar 2, 2018
Javascript Interview Question: Revealing Module Pattern
View revealing-module-pattern.js
 /* Use the module pattern to group together similar methods such as database services. In the example below, the ItemDao (Data Access Object) simulates several functions that interact with a database. 1. Use modules to encapsulate related functionality. 2. A module pattern at its core is an object literal. 3. When using the module in code, call the functions using the key/value syntax. For example: itemDao.findOne(1); 4. Wrap the object into a function to create a closure for private variables. Examples
You can’t perform that action at this time.