# Mały projekt - wybór koncepcji przechowywania wartości

0

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();
});
``````
0

Obie wersje są koszmarne ze wzgledu na drabinke `if`ów.
Poza tym oba rozwiazania się tak naprawde niewiele różnia, nie wykorzystujesz w drugim przypadku tego, że masz tablice.
Kolejna rzecz: `isNaN` - serio może tam być `NaN`? `NaN` to produkt niedozwolonych operacji matematycznch takich jak działania na typach innych niż `Number` czy np. `Math.sqrt(-1)`. Jak masz cos takiego w programie to masz powazny problem.
Dalej - po co ci argumenty w konstruktorze, skoro ich nie uzywasz??

Proponuję wrócić do nauki pisania funkcji, operacji na kolekcjach itp, bo jak nie masz podstaw i bierzesz się za bardziej zaawansowane rzeczy to wychodza takie koszmarki.

0

Argumenty w konstruktorze - Tego jeszcze nie skończyłem. Chodziło mi o to, które z tych rozwiązać jest lepsze.
Mogę mieć taką sytuację, że zapomnę wpisać jeden z wag
np.
1 op./ 400 g
1 kg - 34,23 zł - 23,23 zł
i program musi zgłosić, że brakuje wagi - i w drugą stronę
1 op./ 400 g - 500 g
1 kg - 32,34 zł
że zapomniałem przeliczeniówki - stąd te Ify

0

Masz rację pozbyłem się tych ifów - jeszcze pewne rzeczy "stanowią "toDO", ale się klaruje chyba ładnie.

``````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));
};
Array.prototype.forEach = function (func) {
for (var i = 0; i < this.length; i++)
func(this[i]);
};
Array.prototype.map = function (func) {
var result = [];
this.forEach(function(element) {
result.push(func(element));
});
return result;
};

Array.prototype.indexOf = function(elt /*, from*/) {
var len = this.length;

var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;

for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};

function Module(price, desc) {
this.price = price;
this.desc = desc;
this.weight = this.extWeight();
this.uPrice = this.extUprice();
}

Module.prototype = {
check: function () {
this.result = [];
var len = Math.max(this.weight.length, this.uPrice.length);
for (var i = 0 ; i < len ; i++)
this.result.push(calc(this.price, this.weight[i], this.uPrice[i]));
},
extWeight: function () {return 2},
extUprice: function () {return 20}
};
function calc (price, weight, uPrice) {
var ratio = (price * 1e3 / weight).roundUp(2);
return isNaN(ratio) ? "noWeight" : (ratio == uPrice ? "OK" : ratio.toFixed(2) + " zł");
}

var mod1 = new Module();
mod1.price = 6.99;
mod1.weight = [350];
mod1.uPrice = [34.23];

var mod2 = new Module();
mod2.price = 3.99;
mod2.weight = [400, 500];
mod2.uPrice = [9.98, 7.98];

var arr = [];
arr[0] = mod1;
arr[1] = mod2;

arr.forEach(function (obj){
obj.check();
});

var data = retrieveGroups ();
``````