-
-
Save sadasant/3762517 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require('assert') | |
/* | |
Fizzbuzz | |
*/ | |
function FizzBuzz (number) { | |
if (number % 3 == 0 && number % 5 == 0) return 'fizzbuzz' | |
else if (number % 3 == 0) return 'fizz' | |
else if (number % 5 == 0) return 'buzz' | |
else return number | |
} | |
/* Prueba */ | |
assert(FizzBuzz(15), 'fizzbuzz', '15 es multiplo de ambos') | |
assert.equal(FizzBuzz(4), 4, '4 no es multiplo de ninguno') | |
assert(FizzBuzz(9), 'fizz', '9 es multiplo de 3') | |
assert(FizzBuzz(25), 'buzz', '25 es multiplo de 5') | |
/* del 1 al 100 sin ciclos, condiciones */ | |
;(function Print(end) { | |
// console.log(end--) | |
// return end && Print(end) | |
var a = new Array(end) | |
function loop (ind) { | |
console.log(end - a.length) | |
return a.length && loop(a.pop()) | |
} | |
loop(a.pop()) | |
})(100) | |
/* */ | |
function fn (str) { | |
var bk = str | |
return function f (st) { | |
if (arguments.length) { | |
bk += st | |
return f | |
} else { | |
return bk | |
} | |
} | |
} | |
assert(fn('Hola ')('VenezuelaJS')() + fn('!!!')(), 'Hola VenezuelaJS!!!', 'Deben ser Iguales') | |
/* Promesas */ | |
function promiseChain (cb) { | |
var org = cb | |
var ar = [] | |
var errors = [] | |
return function p (act) { | |
if (typeof(act) == 'function' && arguments.length) { | |
ar.push(act) | |
return p | |
} else { | |
var rcb = ar.pop() | |
while(fn = ar.shift()) { | |
if (errors.length) continue | |
fn.call(this, function (err) { | |
if (err) { | |
errors.push(err) | |
} | |
}) | |
} | |
if (errors.length) { | |
rcb.call(this, errors[0]) | |
} else { | |
rcb.call(this, null, true) | |
} | |
} | |
} | |
} | |
function test (cb) { | |
promiseChain(cb)(function(cb){ | |
return cb(new Error('Este es un error')) | |
})(function (cb){ | |
console.log('esto no deberia de correr') | |
return cb() | |
})(cb)() | |
} | |
function test2 (cb) { | |
promiseChain(cb)(function(cb){ | |
return process.nextTick(cb) | |
})(function (cb){ | |
var i = 100,c = 0 | |
while (--i) { | |
c = i | |
} | |
process.nextTick(cb) | |
})(cb)() | |
} | |
test(function(error){ | |
assert(error.message, 'Este es un error', 'Deberia retornar un error') | |
test2(function(err, res ){ | |
assert(res, true, '(res) debe ser true') | |
}) | |
}) | |
function sort(arr) { | |
var length = arr.length, | |
offset = Math.min.apply(Math, arr) | |
for(item in list) { | |
arr[arr[item] + length - offset] += '.' + arr[item] | |
} | |
return arr.join(',') | |
.replace(/undefined/g, '') | |
.replace(/,+/g, '.') | |
.replace(/\.+/g, '.') | |
.slice(1) | |
.split('.') | |
.slice(length) | |
} | |
assert.deepEqual(sort([0,2,3,1,3]), [0,1,2,3,3], 'Deberia de ordenarlas') | |
/*Alterar objetos */ | |
function alterarObjeto (Class, property) { | |
return Class.__defineGetter__('greeting', function () { | |
return property | |
}) | |
} | |
function C (name) { | |
this.name = name | |
this.greeting = 'hola' | |
return this | |
} | |
var obj1 = new C('Aleja') | |
var obj2 = new C('Alejo') | |
var greeting = 'mundo' | |
assert.equal(obj1.greeting, 'hola') | |
assert.equal(obj2.greeting, 'hola') | |
alterarObjeto(C, greeting) | |
assert(obj1.greeting, greeting) | |
assert(obj2.greeting, greeting) | |
assert(new C('Pedro').greeting, greeting) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment