hej,
napisałem dwa kody
czy mogę prosić o wybór lepszego z nich - nie mam takiego doświadczenia (żadnego w sumie nie mam :)
Mile widziane komentarze :)) (poproszę)
Koncepcja pierwsza opiera się na predefiniowanych nazwach własności takich jak: weight, weight_1, unitPrice_1
być może
Number.prototype.roundUp = function (prec) {
var dotFive = /\d+\.\d\d5.?/;
if (dotFive.test(this))
return Math.ceil (this * Math.pow(10, prec)) / Math.pow(10, prec);
else
return Number (this.toFixed(prec));
};
function Module (price, desc) {
this.price = price;
this.desc = desc;
}
function ModuleSingle(price, weight, uPrice, desc) {
this.price = price;
this.weight = weight;
this.uPrice = uPrice;
this.desc = desc;
}
function ModuleMulti(price, weight_1, weight_2, uPrice_1, uPrice_2, desc) {
this.price = price;
this.weight_1 = weight_1;
this.weight_2 = weight_2;
this.uPrice_1 = uPrice_1;
this.uPrice_2 = uPrice_2;
this.desc = desc;
}
ModuleSingle.prototype.check = function () {
if (isNaN(this.weight))
return this.result = "no weight";
else if (isNaN(this.uPrice))
return this.result = "no uPrice";
else
this.result = calc(this.price, this.weight, this.uPrice);
};
ModuleMulti.prototype.check = function () {
if (isNaN(this.weight_1))
this.result_1 = "no lower weight";
else if (isNaN(this.uPrice_1))
this.result_1 = "no lower uPrice";
else
this.result_1 = calc(this.price, this.weight_1, this.uPrice_1);
if (isNaN(this.weight_2))
this.result_2 = "no higher weight";
else if (isNaN(this.uPrice_2))
this.result_2 = "no higher uPrice";
else
this.result_2 = calc(this.price, this.weight_2, this.uPrice_2);
};
function calc(price, weight, uPrice) {
var ratio = (price * 1e3 / weight).roundUp(2);
return ratio == uPrice ? "OK" : ratio.toFixed(2) + " zł";
}
Array.prototype.forEach = function (func) {
for (var i = 0; i < this.length; i++)
func(this[i]);
}
var mod1 = new ModuleSingle();
mod1.price = 6.99;
mod1.weight = 350;
mod1.uPrice = 19.97;
var mod2 = new ModuleMulti();
mod2.price = 3.99;
mod2.weight_1 = 400;
mod2.weight_2 = 550;
mod2.uPrice_1 = 9.98;
//mod2.uPrice_2 = 7.25;
var arr = [];
arr[0] = mod1;
arr[1] = mod2;
arr.forEach(function (obj){
obj.check();
});
drugi kod oparty na koncepcji zapisywania wag i unitPrice(przeliczeniówek w tablicach:
Number.prototype.roundUp = function (prec) {
var dotFive = /\d+\.\d\d5.?/;
if (dotFive.test(this))
return Math.ceil (this * Math.pow(10, prec)) / Math.pow(10, prec);
else
return Number (this.toFixed(prec));
};
function Module(price, weight, uPrice, desc) {
this.price = price;
this.weight = weight;
this.uPrice = uPrice;
this.desc = desc;
}
Module.prototype.check = function () {
this.result = [];
var len = Math.max(this.weight.length, this.uPrice.length);
if (len == 1) {
if (isNaN(this.weight[0]))
return this.result = "no weight";
else if (isNaN(this.uPrice[0]))
return this.result = "no uPrice";
else
this.result.push(calc(this.price, this.weight[0], this.uPrice[0]));
}
else if (len == 2) {
if (isNaN(this.weight[0]))
this.result.push("no lower weight");
else if (isNaN(this.uPrice[0]))
this.result.push("no lower uPrice");
else
this.result.push(calc(this.price, this.weight[0], this.uPrice[0]));
if (isNaN(this.weight[1]))
this.result.push("no higher weight");
else if (isNaN(this.uPrice[1]))
this.result.push("no higher uPrice");
else
this.result.push(calc(this.price, this.weight[1], this.uPrice[1]));
}
};
function calc(price, weight, uPrice) {
var ratio = (price * 1e3 / weight).roundUp(2);
return ratio == uPrice ? "OK" : ratio.toFixed(2) + " zł";
}
Array.prototype.forEach = function (func) {
for (var i = 0; i < this.length; i++)
func(this[i]);
}
var mod1 = new Module();
mod1.price = 6.99;
mod1.weight = [350];
mod1.uPrice = [19.97];
var mod2 = new Module();
mod2.price = 3.99;
mod2.weight = [400, 500];
mod2.uPrice = [undefined, 7.78];
var arr = [];
arr[0] = mod1;
arr[1] = mod2;
arr.forEach(function (obj){
obj.check();
});