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
/* | |
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
/* | |
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
/* | |
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 |
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 of orders, find the top 3 customers with the | |
* highest lifetime value (those that spent the most over time). | |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
// * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
// Start with the initial implementation. | |
// | |
// One approach to solving this task would be to create |
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
* Write a program to find a fibonacci number. | |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
// * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
// Iterative solution | |
// O(n) Linear Runtime Complexity | |
// This is an alternative approach to solving fibonacci | |
// without recursion. However, this shoudn't be the | |
// final answer in an interview setting. |
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
* Solve some arbitrary problem using a queue. | |
* The interviewer has asked to provide a solution to a problem | |
* using a Queue as the data structure. Using the native Array | |
* in JavaScript with restricted access to its full list of methods | |
* is one possible solution. | |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
//Queues implement the FIFO method. First in, first out. | |
class Queue { |
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 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 = []; | |
} |
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
/* | |
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. | |
*/ |
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
/* | |
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; |