Skip to content

Instantly share code, notes, and snippets.

@varmais
Last active February 3, 2017 11:52
Show Gist options
  • Save varmais/3b3284ae4e5c57629843723ccc04f383 to your computer and use it in GitHub Desktop.
Save varmais/3b3284ae4e5c57629843723ccc04f383 to your computer and use it in GitHub Desktop.
Talented problems
Array.range = function(start, count) {
return Array.apply(null, Array(count)).map((e, i) => start + i);
}
Array.prototype.sum = function() {
return this.reduce((prev, next) => prev + next, 0);
}
function getCard() {
const bCards = createCards(5, 'B', 1, 15);
const iCards = createCards(5, 'I', 16, 30);
const nCards = createCards(4, 'N', 31, 45);
const gCards = createCards(5, 'G', 46, 60);
const oCards = createCards(5, 'O', 61, 75);
return [].concat.apply(bCards, [iCards, nCards, gCards, oCards]);
}
function createCards (amount, letter, min, max) {
const cards = [];
for (let i = 0; i < amount; i++) {
let element;
while (!element || cards.includes(element)) {
element = letter + getRandomInt(min, max);
}
cards.push(element);
}
return cards;
}
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function CarOld (model, registerNumber, color) {
this.color = color;
this.paint = function (color) {
this.color = color;
};
Object.defineProperties(this, {
'model': {value: model, writable: false},
'registerNumber': {value: registerNumber, writable: false},
'color': {value: this.color},
'paint': {value: this.paint}
})
}
class Car {
constructor (model, registerNumber, color) {
this._model = model;
this._registerNumber = registerNumber;
this._color = color;
}
paint (color) {
this.color = color;
}
get model () {
return this._model;
}
get registerNumber () {
return this._registerNumber;
}
set color (color) {
this._color = color;
}
get color () {
return this._color;
}
}
function fib (n, x, y) {
if (n < 1) return;
console.log(n, x);
n--;
fib(n, y, x+y);
}
fib (100, 0, 1);
function flattenArray (arr) {
return arr.reduce((acc, next) => {
if (Array.isArray(next)) {
return acc.concat(flattenArray(next));
} else {
return acc.concat(next);
}
}, []).filter(it => !!it);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment