Skip to content

Instantly share code, notes, and snippets.

View jswhisperer's full-sized avatar
🕵️
For Sale... I mean open to work.

Gregory The JSWhisperer jswhisperer

🕵️
For Sale... I mean open to work.
View GitHub Profile
@jswhisperer
jswhisperer / Truncate an array using length.js
Created January 4, 2014 18:26
Truncate an array using length
var myArray = [12 , 222 , 1000 , 124 , 98 , 10 ];
myArray.length = 4; // myArray will be equal to [12 , 222 , 1000 , 124]
@jswhisperer
jswhisperer / Get the max or the min in an array of numbers.js
Created January 4, 2014 18:24
Get the max or the min in an array of numbers
var numbers = [5, 458 , 120 , -215 , 228 , 400 , 122205, -85411];
var maxInNumbers = Math.max.apply(Math, numbers);
var minInNumbers = Math.min.apply(Math, numbers);
@jswhisperer
jswhisperer / Verify that a given argument is a number.js
Created January 4, 2014 18:21
Verify that a given argument is a number
function isNumber(n){
return !isNaN(parseFloat(n)) && isFinite(n);
}
@jswhisperer
jswhisperer / Get a random item from an array.js
Created January 4, 2014 18:14
Get a random item from an array
var items = [12, 548 , 'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' , 2145 , 119];
var randomItem = items[Math.floor(Math.random() * items.length)];
@jswhisperer
jswhisperer / create a stylesheet
Created July 5, 2013 12:17
Create a stylesheet with JS
var sheet = (function() {
// Create the <style> tag
var style = document.createElement("style");
// Add a media (and/or media query) here if you'd like!
// style.setAttribute("media", "screen")
// style.setAttribute("media", "@media only screen and (max-width : 1024px)")
// WebKit hack :(
style.appendChild(document.createTextNode(""));
@jswhisperer
jswhisperer / closestNumber
Created May 9, 2013 10:34
Pass an array and a value, get back the closest number.
var getClosestNum = function(num, ar) {
var i = 0, closestDiff, currentDiff;
if(ar.length) {
closest = ar[0];
for(i;i<ar.length;i++) {
closestDiff = Math.abs(num - closest);
currentDiff = Math.abs(num - ar[i]);
if(currentDiff < closestDiff) {
closest = ar[i];
}
@scopevale
scopevale / html5boilerplate.html
Created January 20, 2012 20:36
HTML5 Boilerplate
<!doctype html>
<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ -->
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="no-js ie7 oldie" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="no-js ie8 oldie" lang="en"> <![endif]-->
<!-- Consider adding an manifest.appcache: h5bp.com/d/Offline -->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
@karthikshiraly
karthikshiraly / myapp.html
Created January 11, 2012 08:18
Very simple, basic backbone.js example with comments - Demonstrates how to get a value from one porition of UI and update it in another portion of UI via the model
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Backbone.js example</title>
<link rel="stylesheet" type="text/css" href="css/sunny/jqueryui.min.css"/>
</head>
<body>
<!-- "slider" is a jquery slider -->
<div id="slider"></div>