JSFiddle - React, Tailwind, and code Playground
by Roman Zhak
JavaScript
(function(){
function fillX(step, count, start) {
var i = 0, values = start || 0;
step = step || 1;
this.resX = [];
while( i <= count ) {
this.resX.push(values);
values += step;
i++;
}
return this;
}
function fillY(fn) {
var self = this;
this.resY = [];
this.resX.forEach(function(x){
self.resY.push(fn(x))
});
return this;
}
function closest(num) {
// array is already sorted
// according the method fillX
var curr = this.resX[0];
var diff = Math.abs (num - curr);
for (var val = 0; val < this.resX.length; val++) {
var newdiff = Math.abs (num - this.resX[val]);
if (newdiff < diff) {
diff = newdiff;
curr = this.resX[val];
}
}
return eq(curr);
}
function get(index) {
return this.resY[index];
}
function fill() {
var size = this.resY.length;
var matrix = new Array(size);
matrix[0] = this.resY;
// fill others
for(var i = 1; i < size; i++) {
var rows = matrix[i] = new Array(i)
.join('-').split('-');
for(var j = i - 1; j < size; j++) {
var curr = matrix[ i - 1 ][j];
var next = matrix[ i - 1 ][j + 1];
// console.log('Curr = %s, Next = %s', curr, next)
if( next ) rows.push(next - curr)
}
}
return matrix;
}
function render(matrix) {
var table = $('<table border=1></table>');
matrix.forEach(function(row, index){
var tr = $('<tr></tr>');
for(var i = 0; i < row.length; i++) {
tr.append(
'<td width="100px">' +
( typeof row[index] === 'number' ? row[i] : 0 )
+'</td>')
table.append(tr);
}
})
table.appendTo('body');
}
// public API
this['fill'] = {
x : fillX,
y : fillY,
closest : closest,
get : get,
fill : fill,
render : render
}
}).call(this);
function factorial(num) {
var rval=1;
for (var...