Skip to content

Instantly share code, notes, and snippets.

@misterussell
misterussell / thisTest
Last active October 20, 2016 12:59
Test of JS this
// Problem 1
// console.log(this);
// this is: window object or global scope
// because... this is in the global scope
// // Problem 2
var obj1 = {
myName: 'obj1',
this: this
};
@misterussell
misterussell / deletebutton.js
Last active October 26, 2016 13:59
Example of a user specific delete button.
var settings = {
url: '<<server link here>',
type: 'GET',
success: (data, status, xhr) => {
//THE forEach loop below runs through our server, pulling all current data to display.
data.forEach( (message, i, arr) => {
let messageLine = $(`
<li class=>
<span class="postDetails">${message.userName} (${moment(message.timeStamp).format('DD/MM/YY, h:mm a')}):</span>
<span class="postBody">${message.body}</span>
@misterussell
misterussell / arraySplitter.js
Created November 15, 2016 16:09
Splits an array into subarrays of a specified length.
function subArray(array, length) {
// get an array
// for each over the array
// if the index is length spots in
// the numbers it traversed should be kept in an
// new array
let storage = [];
let i = 0;
//
while (storage.length < (array.length / length)) {
@misterussell
misterussell / confirmEnding.js
Created November 16, 2016 16:09
A function that confirms the end of a string from a specified parameter.
// function to confirm the ending of a string
function confirmEnding(s, eS) {
// look at the end string (s)
// look at the end of the end string (eS)
// compare the index of the last of both
// cycle over the two string simulteanously
// if gets to the beginning of endString and nothing has been false
// return true
let splitString = s.split('');
@misterussell
misterussell / rot13decoder.js
Created November 17, 2016 16:54
Rot13 decoder
function rot13(S) {
//create our alphabet
let az = 'abcdefghijklmnopqrstuvwxyz'.toUpperCase().split('');
//create an array for the string passed in
let splitS = S.split(' ');
//recreate the new arrays
let newString = splitS.map((word) => {
let splitWord = word.split('');
let newWord = splitWord.map((letter) => {
// when letter = az
@misterussell
misterussell / uniqueIndices.js
Created November 21, 2016 15:31
Function to return unique values from two arrays
function diff(arr1, arr2) {
// put the arrays together
// loop over the data
combinedArr = arr1.concat(arr2);
// filter over the combined array and compare the index and last index of each number
// if they match then return that number
let combedArray = combinedArr.filter((num) => {
if (combinedArr.indexOf(num) === combinedArr.lastIndexOf(num)) {
return num;
}
@misterussell
misterussell / sideEffectMin
Created June 13, 2017 15:17
Practice exercise in moving away from for loops while minimizing side effects.
const waffles = [
{ name: 'Chocolate Chip', calories: 800 },
{ name: 'Blueberry', calories: 400 },
{ name: 'Whole Grain', calories: 350 },
{ name: 'Savory', calories: 600 }
];
const isHealthy = waffle => waffle.calories < 200;
const getName = waffle => waffle.name;
const getItemNames = items => items.filter(isHealthy).map(getName);
@misterussell
misterussell / codewarsOne
Created June 14, 2017 18:15
Codewars Test 1
function checkIfArray(arr) {
if (arr.constructor === Array ) {
return arr;
} else return convertToArray(arr);
}
function convertToArray(string) {
return Array.from(string);
}
@misterussell
misterussell / codewarsTwo
Created June 14, 2017 20:34
Codewars Test 2
function isValidWalk(walk) {
let compass = {};
walk.forEach((direction, i) => {
if (compass[direction]) {
compass[direction] += 1;
} else {
compass[direction] = 1;
}
});
@misterussell
misterussell / codewarsThree
Created June 15, 2017 17:00
Codewars Test 3
function combineValues(data) {
// compute the value of the last 3 vals
return getLastThree(data).reduce((a, b) => {
return a + b;
}, 0);
}
function getLastThree(data) {
// if the length is 3 we don't need to pull any specific locations as we are computing against all of these
if (data.length > 3) {