Skip to content

Instantly share code, notes, and snippets.

@jp26jp
jp26jp / clickOutsideDiv.js
Created January 21, 2019 22:49
Hides a div when a click occurs outside of the specified container
$(document).click(function(e) {
let container = $("YOUR_CONTAINER_SELECTOR");
// if the target of the click isn't the container nor a descendant of the container
if (!container.is(e.target) && container.has(e.target).length === 0) {
container.hide();
}
});
@jp26jp
jp26jp / addScript.js
Created December 5, 2018 07:22
Add a javascript file to the body of the html document
function addScript(url) {
const tag = document.createElement("script")
tag.type = "text/javascript"
tag.src = url
document.body.appendChild(tag)
}
// start at the beginning of the array and iterate through to the second to last item
for (int currentIndex = 0; currentIndex < array.length - 1; currentIndex++)
{
// assume the currentIndex is the smallest
int minIndex = currentIndex;
// set i to be the index immediately proceeding the currentIndex
for (int i = currentIndex + 1; i < array.length; i++)
{
// if true, we found a smaller index
if (array[i] < array[minIndex])
@jp26jp
jp26jp / GenerateDOT.java
Last active October 24, 2018 22:16
Generates a string containing all of the edges in the tree rooted at "this" node, in DOT format.
/**
* Generates a string containing all of the edges in the tree rooted at "this" node, in DOT format.
* Assumes this node has member variables called “data”, “leftChild”, and “rightChild”.
*
* @return DOT format string to enter at http://www.webgraphviz.com
*/
public String generateDot() throws Exception
{
// `root` represents the root node of the binary search tree
if (root == null)
@jp26jp
jp26jp / MergeSort.java
Last active February 22, 2018 20:59
Merge sort algorithm using an ArrayList
void mergeSort(final ArrayList list, final int start, final int end)
{
if (start >= end)
return;
int middle = (start + end) / 2;
mergeSort(list, start, middle); // Sort the left side of the list
mergeSort(list, middle + 1, end); // Sort the right side of the list
merge(list, new ArrayList<>(list.size()), start, middle + 1, end + 1); // Merge both sides
@jp26jp
jp26jp / SelectionSort.java
Last active February 22, 2018 16:11
Selection sort algorithm
void selectionSort(int[] array)
{
for (int i = 0; i < array.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < array.length; j++)
if (array[j] < array[index])
index = j;
int temp = array[i];
@jp26jp
jp26jp / InsertionSort.java
Last active February 22, 2018 16:10
Insertion sort algorithm
void insertionSort(int[] array)
{
for (int i = 1; i < array.length; i++)
{
int index = i, indexValue = array[index];
while (index > 0 && array[index - 1] > indexValue) array[index] = array[--index];
array[index] = indexValue;
}
@jp26jp
jp26jp / smooth-scroll.js
Created December 4, 2017 19:31
Smooth scrolling
$('a[href*="#"]')
// Remove links that don't actually link to anything
.not('[href="#"]')
.not('[href="#0"]')
.click(function(event) {
// On-page links
if (
location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
&&
location.hostname == this.hostname
@jp26jp
jp26jp / .java
Last active September 16, 2017 21:55
/**
* We figured out part of the answer to our question.
* So now we'd just like to know if we're on the right track.
*/
public void insert(int index, E data)
{
ensureCapacity(indexCounter + 1);
for (int i = elementData.length; i > index; i--)
@jp26jp
jp26jp / .js
Created May 8, 2017 18:09
Scroll only in hovered div
$('#menu').on('DOMMouseScroll mousewheel', function(ev) {
var $this = $(this),
scrollTop = this.scrollTop,
scrollHeight = this.scrollHeight,
height = $this.height(),
delta = (ev.type == 'DOMMouseScroll' ?
ev.originalEvent.detail * -40 :
ev.originalEvent.wheelDelta),
up = delta > 0;