Skip to content

Instantly share code, notes, and snippets.

// taken from http://www.dreamincode.net/code/snippet154.htm
//
// Find the weaknesses, and clean up the code.
// Find at least 3 things you can make better!
//
// Test numbers (return true):
// 4111111111111111
// 378282246310005
// 5555555555554444
Number.prototype.times = function(string){
var result = '', times = this;
while(times--) {
result += string;
}
return result;
}
document.write((4).times('test'));
// Exercise 2 - Closures
// Wrap the following code in a closure and export only the "countdown" function.
// Code
var countdown = (function(){
var index;
function log(){
console.log(index);
}
function logCar(obj, objName) {
var result = "";
// Object.keys(obj).length
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
result += obj[i] + " ";
}
}
console.log("My car is a " + result);
}
var mercedes = ({ color: 'red', make: 'Mercedes' });
var ferrari = ({ color: 'red', make: 'Ferrari' });
function logCar(car){
return "I'm a " + car.color + " " + car.make;
}
logCar(mercedes);
logCar(ferrari);
function Car(color,make) {
this.color = color;
this.make = make;
this.log = function(){
console.log("My car is a " + this.color + " " + this.make + ".");
};
}
(new Car('blue','BMW')).log();
(new Car('green','Ferrari')).log();
Function.prototype.cached = function(){
// reference original function and create cache
var self = this, cache = {};
return function(arg){
// is this argument already in the cache?
if(arg in cache) return cache[arg];
// if not add it
return cache[arg] = self(arg);
}
}
// A tracing version:
Function.prototype.cached = function(){
var self = this, cache = {};
return function(arg){
if(arg in cache) {
console.log('Cache hit for '+arg);
return cache[arg];
} else {
console.log('Cache miss for '+arg);
// 1. Write a class to support the following code:
function Person(name){
this.name = name;
}
var thomas = new Person('Thomas');
var amy = new Person('Amy');
thomas.name // --> "Thomas"
// 2. Add a getName() method to all Person objects, that outputs
@nerdpruitt
nerdpruitt / SassMeister-input-HTML.html
Created March 20, 2014 19:58
Generated by SassMeister.com.
<div class="tooltip">
<span class="tooltip__icon"></span>
<h2 class="tooltip__heading">This is the heading</h2>
<p class="tooltip__text">This is some body text that can get kinda long sometimes.</p>
<div class="tooltip__meta">Created today</div>
</div>
<br><br>
<div class="tooltip--below">