JSFiddle - React, Tailwind, and code Playground
by Darby Rathbone
JavaScript
var Num = function (value) {
var vals = value.split(/(\-)?(\d*)(?:\.)?(\d*)?/);
this.negative = vals[1] || "";
this.integers = vals[2] || "0";
this.decimal = vals[3] || "0";
};
Num.prototype.toString = function () {
return this.negative + this.integers + '.' + (this.decimal || "0");
};
var Add = function (v1, v2) {
return v1 + v2;
}, Subtract = function (v1, v2) {
return v1 - v2;
}, Multiply = function (v1, v2) {
return v1 * v2;
}, Divide = function (v1, v2) {
return v1 / v2;
}, inOrder = function (v1, v2, d, f, r) {
var int1 = v1.split(''),
int2 = v2.split(''),
solution = '';
r = r || 0;
for (var i1 = parseFloat(d.call(int1)) || 0, i2 = parseFloat(d.call(int2)) || 0; i1 || i2 || r; i1 = parseFloat(d.call(int1)) || 0, i2 = parseFloat(d.call(int2)) || 0) {
if(i1<i2)
var int = f( Add(r,i1), i2) + '';
console.log(r, i1, i2,int);
r = int.slice(0,1) === '-' ? -1 : parseFloat(int.slice(0, -1)) || 0;
solution = r.slice(0,1)==='-'?(10- parseFloat(int.slice(-1))):int.slice(-1) + "" + solution;
}
return solution;
};
var add = function (v1, v2) {
if (v1.negative !== v2.negative) {
if (v1.negative === '-') {
v1.negative = '';
return subtract(v2, v1);
} else {
v2.negative = '';
return subtract(v1, v2);
}
}
var d1 = v1.decimal,
d2 = v2.decimal;
while (d1.length - d2.length) {
if (d1.length < d2.length) d1=d1+('0');
else d2=d2+('0');
}
var d = inOrder(d1, d2, [].pop, Add);
var solution = inOrder(v1.integers, v2.integers, [].pop, Add, parseFloat(d.slice(0, -1 * d1.length)))
+ "." + d.slice(d1.length * -1);
return new Num(v1.negative + solution);
};
var subtract = function (v1, v2) {
if (v1.negative !== v2.negative) {
if (v1.negative === '-') {
v2.negative = '-';
return add(v1, v2);
...