Last active
August 29, 2015 14:01
-
-
Save willmendesneto/77527405b2793163dc3c to your computer and use it in GitHub Desktop.
PrintByDivisors: my resolution for FizzBuzz problem
This file contains hidden or 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 PrintByDivisors = { | |
opts: { | |
begin: 1, | |
end: 100, | |
numbers: [15, 3, 5], | |
outputs: ['SouDev', 'Sou', 'Dev'] | |
}, | |
extend: function(target, source) { | |
target = target || {}; | |
for (var prop in source) { | |
if (typeof source[prop] === 'object') { | |
target[prop] = this.extend(target[prop], source[prop]); | |
} else { | |
target[prop] = source[prop]; | |
} | |
} | |
return target; | |
}, | |
init: function(opts){ | |
if (typeof opts === 'object') { | |
this.extend(this.opts, opts); | |
} | |
for (var number = this.opts.begin; number <= this.opts.end; number++) { | |
console.log(this.getOutputByNumber(number)); | |
} | |
}, | |
getOutputByNumber: function(number){ | |
var stringReturn = number, | |
numbersLength = this.opts.numbers.length | |
; | |
for (var divisor = 0; divisor < numbersLength; divisor++) { | |
if (this.isMultiple(number, this.opts.numbers[divisor])) { | |
stringReturn = typeof stringReturn !== 'number' ? stringReturn + this.opts.outputs[divisor] : this.opts.outputs[divisor]; | |
//break; | |
} | |
} | |
return stringReturn; | |
}, | |
isMultiple: function(n, d) { | |
return n % d === 0; | |
} | |
}; | |
//PrintByDivisors.init(); | |
// Passing params | |
//PrintByDivisors.init({ | |
// begin: 1, | |
// end: 100, | |
// numbers: [20, 15, 10, 3, 5], | |
// outputs: ['Vinte', 'Quinze', 'Dez', 'Tres', 'Cinco'] | |
//}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment