Instantly share code, notes, and snippets.

Gregg GreggSetzer

  • USA
View GitHub Profile
@GreggSetzer
GreggSetzer / vowel.js
Last active Jan 30, 2019
JavaScript Interview Question: Find all Vowels in a string
View vowel.js
/*
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.
@GreggSetzer
GreggSetzer / ip.txt
Created Aug 29, 2018
Retrieve your local IP address at Mac Command LIne
View ip.txt
alias ip='ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2'
@GreggSetzer
GreggSetzer / es6-pyramid.js
Last active Aug 3, 2018
Javascript Interview Question: Pyramid
View es6-pyramid.js
/*
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.
*/
@GreggSetzer
GreggSetzer / tree.js
Last active Jul 27, 2018
JavaScript Interview Question: Tree
View tree.js
class Node {
constructor(data) {
this.data = data;
this.children = [];
}
add(data) {
this.children.push(new Node(data));
}
@GreggSetzer
GreggSetzer / linked-list.js
Last active Jul 27, 2018
JavaScript Interview Question: Linked List
View linked-list.js
/*
Linked List example.
*/
/**
* Node class
* Contains two properties and no methods.
* data - The data to be stored in a single node.
* next - The reference to the next node in the chain.
*/
@GreggSetzer
GreggSetzer / find-largest-integer-in-array.js
Created Jul 16, 2018
JavaScript Interview Question: Largest integer in array?
View find-largest-integer-in-array.js
/*
Return the largest value in an array of non-negative integers
*/
function largestInt(arr) {
if (arr.length === 0) {
return -1;
}
return arr.reduce(comparator);
@GreggSetzer
GreggSetzer / equality.js
Created Jul 16, 2018
JavaScript Interview Questions: Equality
View equality.js
/*
Write a function that determines whether two integers are equal without using any comparison operators.
Use the bitwise XOR operator.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_XOR
Using XOR, if any of the bits are different, a 1 is returned, otherwise, a 0 is returned.
*/
function isEqual(n1, n2) {
return (n1 ^ n2) === 0;
@GreggSetzer
GreggSetzer / es6-steps.js
Last active Jul 15, 2018
Javascript Interview Question: Stairs
View es6-steps.js
/*
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);
}
}
@GreggSetzer
GreggSetzer / spiral-matrix.js
Last active Jul 7, 2018
Javascript Interview Question: Spiral Matrix
View spiral-matrix.js
/*
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.
@GreggSetzer
GreggSetzer / queue-stack.js
Created Jun 22, 2018
JavaScript Interview Question: Create a Queue using Stacks
View queue-stack.js
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Create a Queue implementation using two stacks. Do not use an
* array inside the Queue class. Queue should implement add() and
* remove().
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
class Stack {
constructor() {
this.data = [];
}