Skip to content

Instantly share code, notes, and snippets.

function makeBoard(n) {
var board = []
for (var i = 0; i < n; i++) {
board.push([])
for (var j = 0; j < n; j++) {
board[i].push(false)
}
}
board.togglePiece = function(i, j) {
this[i][j] = !this[i][j]
@ccnixon
ccnixon / balancedParens.js
Created December 26, 2015 17:15
Balanced Parens
function isBalanced (string) {
var queue = [];
for(var i = 0; i < string.length; i++){
if(string[i] === "("){
queue.push(string[i]);
}
if(string[i] === ")"){
queue.pop();
}
}
@ccnixon
ccnixon / linked-list-reverse.js
Created January 13, 2016 01:28
Reverse a linked list in place
var reverseList = function(list){
var tempHead = list.head;
var tempTail = list.tail;
var prevNode = list.head
var currentNode = this.head.next;
while(currentNode) {
var holder = currentNode.next;
currentNode.next = prevNode;
prevNode = currentNode;
currentNode = holder
@ccnixon
ccnixon / binary-search-tree.js
Created January 13, 2016 01:29
Binary Search Tree in JS
var BinarySearchTree = function(value){
var instance = Object.create(binaryTreeMethods);
instance.value = value
instance.left = null;
instance.right = null;
return instance;
@ccnixon
ccnixon / findLongestWord.js
Created February 9, 2016 19:48
Find the Longest Word in a String: Return the length of the longest word in the provided sentence.
function findLongestWord(str) {
str = str.split(' ');
return str.reduce(function(a,b ){ return a.length > b.length ? a : b;}).length
}
findLongestWord("The quick brown fox jumped over the lazy dog");
@ccnixon
ccnixon / end.js
Created February 9, 2016 20:21
Confirm the Ending: Check if a string (first argument) ends with the given target string (second argument).
function end(str, target) {
return str.substring(str.length - target.length) === target;
}
end("Bastian", "n");
@ccnixon
ccnixon / truncateStr.js
Created February 9, 2016 20:41
Truncate a string: Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a "..." ending.
function truncate(str, num) {
// Clear out that junk in your trunk
return num >= str.length? str: str.slice(0, num - (num <= 3 ? 0 : 3)) + '...';
}
truncate("A-tisket a-tasket A green and yellow basket", 11);
@ccnixon
ccnixon / chunk.js
Created February 17, 2016 18:22
Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.
function chunk(arr, size) {
var results = [];
while(arr.length){
results.push(arr.splice(0,size));
}
return results;
}
chunk(["a", "b", "c", "d"], 2);
@ccnixon
ccnixon / CorsRequest.js
Created February 17, 2016 19:39 — forked from subnetmarco/CorsRequest.js
Sample code for executing an AJAX request using jQuery.
$.ajax({
url: 'MASHAPE-URL', // The URL to the API. You can get this by clicking on "Show CURL example" from an API profile
type: 'POST', // The HTTP Method
data: {}, // Additional parameters here
dataType: 'json',
success: function(data) { alert(JSON.stringify(data)); },
error: function(err) { alert(err); },
beforeSend: function(xhr) {
xhr.setRequestHeader("X-Mashape-Authorization", "YOUR-MASHAPE-KEY"); // Enter here your Mashape key
}
@ccnixon
ccnixon / index.html
Created April 7, 2016 14:26
Simple Event Timeline (React)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Datanyze Challenge</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<style>