Page 3 of 8

how to calculate label values in an array

Posted: Wed Nov 20, 2013 1:17 pm
by Maryna Brodina

There is no localStorage variables price and extrasprice on the last screenshot. Are you sure there are correct names? Please check, perhaps it's just not shown on screenshot.


how to calculate label values in an array

Posted: Wed Nov 20, 2013 1:22 pm
by Michael4771079

Hi Marina,
the variables are in localstorage, see second screenshot, when I run the app nothing shows for "price" and "extrasprice"


how to calculate label values in an array

Posted: Wed Nov 20, 2013 1:39 pm
by Maryna Brodina

The fact you created these variables (second screenshot) doesn't mean you've filled them. As we can see from the last screenshot there are no these variables, that means you doesn't save there anything. As a result there won't be any calculations using these variables.


how to calculate label values in an array

Posted: Wed Nov 20, 2013 1:47 pm
by Michael4771079

So What do you think is the problem, am I running the code properly, on success of rest service?


how to calculate label values in an array

Posted: Wed Nov 20, 2013 2:05 pm
by Maryna Brodina

The problem is that you don't fill in localStorage variables. Please carefully check your app, think about when (on what event) you have to fill in them.


how to calculate label values in an array

Posted: Wed Nov 20, 2013 2:13 pm
by Michael4771079

Sorry Marina,
I dont follow,
the labels I need to calculate are saved in "mainorderArray" and "livesundriesArray"

so I created the variables "price", "extrasprice" and "totalprice" and added the code on success of rest service STARUSERREAD reading "mainorderArray" and "livesundriesArray"

I thought that the code would preform the calculation and the values would be saved to localstorage
can you advise where I am going wrong


how to calculate label values in an array

Posted: Wed Nov 20, 2013 2:45 pm
by Maryna Brodina

If your localStorage arrays names are "mainorderArray" and "livesundriesArray" change code to the following pre var price;
try {
price = JSON.parse(localStorage.getItem("mainorderArray"));
if ({}.toString.call(price) !== "[object Array]") {
price = [];
}
} catch ( e ) {
price = [];
}
var extrasprice;
try {
extrasprice = JSON.parse(localStorage.getItem("livesundriesArray"));
if ({}.toString.call(extrasprice) !== "[object Array]") {
extrasprice = [];
}
} catch ( e ) {
extrasprice = [];
}
var totalprice = [];
if (price.length === extrasprice.length){
for (i = 0; i < price&#46;length; i++){
totalprice&#46;push(+price["Price"] + (+extrasprice["Price"]))
};
}
localStorage&#46;setItem("totalpriceArray", JSON&#46;stringify(totalprice));/prePlease note there is a price.length === extrasprice.length condition in code so arrays size should match. If they don't - calculation won't work. You can delete this condition if you don't need one.


how to calculate label values in an array

Posted: Wed Nov 20, 2013 3:11 pm
by Michael4771079

Ok Maryna,
as my arrays are different in lenght I have deleted some of the code,unsure if I have deleted correctly?
these are the arrays

code var price;
try {
price = JSON&#46;parse(localStorage&#46;getItem("mainorderArray"));
if ({}&#46;toString&#46;call(price) !== "[object Array]") {
price = [];
}
} catch ( e ) {
price = [];
}
var extrasprice;
try {
extrasprice = JSON&#46;parse(localStorage&#46;getItem("livesundriesArray"));
if ({}&#46;toString&#46;call(extrasprice) !== "[object Array]") {
extrasprice = [];
}
} catch ( e ) {
extrasprice = [];
}
var totalprice = [];
{
totalprice&#46;push(+price["Price"] + (+extrasprice["Price"]))
};
}
localStorage&#46;setItem("totalpriceArray", JSON&#46;stringify(totalprice));/code

Image


how to calculate label values in an array

Posted: Wed Nov 20, 2013 6:05 pm
by Kateryna Grynko

Hi Michael,

Replace:pre{
totalprice&#46;push(+price["Price"] + (+extrasprice["Price"]))
};
}/prewith:prefor (i = 0; i < price&#46;length; i++){
totalprice&#46;push(+price["Price"] + (+extrasprice["Price"]))
};/pre


how to calculate label values in an array

Posted: Wed Nov 20, 2013 6:33 pm
by Michael4771079

Hi Katya,
I changed code here it is, and the error in console

Image

Image

Im sure your sick of this issue :)