Skip to content

Instantly share code, notes, and snippets.

View AllThingsSmitty's full-sized avatar

Matt Smith AllThingsSmitty

View GitHub Profile
@AllThingsSmitty
AllThingsSmitty / import.css
Created July 17, 2015 23:15
How our main CSS file might end up looking with HTTP/2
@import "core.css";
@import "theme.css";
@import "user-theme.css";
@import "small-breakpoint.css" (max-width: 40em);
@import "landscape.css" screen and (orientation: landscape);
@import "print.css" print;
@AllThingsSmitty
AllThingsSmitty / last-el.js
Last active August 29, 2015 14:25
Get last element of an array in one line with no temp vars
// var last = arr.reverse()[0];
Array.prototype.last = function () { return this.length > 0 ? this[this.length -1] : undefined };
@AllThingsSmitty
AllThingsSmitty / negative-nth-child.css
Created July 23, 2015 14:45
Use negative nth-child to select items 1 through n
li {
display: none;
}
/* select items 1 through 3 and show them */
li:nth-child(-n+3) {
display: block;
}
@AllThingsSmitty
AllThingsSmitty / exec.js
Last active November 27, 2015 22:55
Testing regex in JavaScript with the exec() method
console.log(/p.*g/.test('programming')); // true
console.log(/p.*g/.test('pickles')); // false
var re = /quick\s(brown).+?(jumps)/ig;
var result = re.exec(
'The Quick Brown Fox Jumps Over The Lazy Dog'
);
console.log(result);
// ["Quick Brown Fox Jumps", "Brown", "Jumps"]
@AllThingsSmitty
AllThingsSmitty / sort-merge.js
Last active November 15, 2015 14:37
Take two sorted lists of numbers and merge them into a single sorted list
var list1 = [2, 4, 5, 6];
var list2 = [3, 5, 8, 9];
merged_list = merge_sorted_list(list1, list2);
alert(merged_list);
function merge_sorted_list(list1, list2) {
merged_list = list1.concat(list2);
return merged_list.sort(numsort);
}
@AllThingsSmitty
AllThingsSmitty / sub-array-sum.js
Last active November 15, 2015 14:36
Given an array of integers (positive or negative) find the sub-array with the largest sum
var list = [
2, -4,
6, -9, [8, 9, -6],
12, [45, 3, 7], -34, [7, -2]
];
sublists = [];
sublists_sum = [];
for (var i in list) {
if (Object.prototype.toString.call(list[i]) == '[object Array]') {
@AllThingsSmitty
AllThingsSmitty / palindrome.js
Last active November 29, 2015 14:29
Determine if a given string is a palindrome
var input_str = 'madam';
function checkIfPalindrome(str) {
if (str == str.split('').reverse().join('')) {
alert('Yup, ' + str + 'is a palindrome');
} else {
alert('Nope, ' + str + ' isn\'t a palindrome');
}
}
@AllThingsSmitty
AllThingsSmitty / truncate.js
Last active November 29, 2015 14:29
Apply ellipsis to the end of content that has more than 300 characters
if (content.length > 300) {
// truncate to a shorter text length
return content.substring(0, 200) + '...';
}
@AllThingsSmitty
AllThingsSmitty / js-terms.md
Last active November 9, 2024 16:01
10 terms to help you better understand JavaScript

10 JavaScript Terms You Should Know

From currying to closures there are quite a number of special words used in JavaScript. These will not only help you increase your vocabulary but also better understand JavaScript. Special terms are normally found in documentation and technical articles. But some of them like closures are pretty standard things to know about. Knowing what the word itself means can help you know the concept it's named for better.

  1. Arity
  2. Anonymous
  3. Closure
  4. Currying
  5. Hoisting
  6. Mutation
@AllThingsSmitty
AllThingsSmitty / live-code.css
Created August 27, 2015 13:08
Making an entire live-code-enabled presentation
<style contenteditable>
* {
display: block;
}
</style>