Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

how to calculate label values in an array

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.

Michael4771079
Posts: 0
Joined: Sat Jul 21, 2012 2:03 pm

how to calculate label values in an array

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

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

how to calculate label values in an array

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.

Michael4771079
Posts: 0
Joined: Sat Jul 21, 2012 2:03 pm

how to calculate label values in an array

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

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

how to calculate label values in an array

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.

Michael4771079
Posts: 0
Joined: Sat Jul 21, 2012 2:03 pm

how to calculate label values in an array

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

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

how to calculate label values in an array

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.

Michael4771079
Posts: 0
Joined: Sat Jul 21, 2012 2:03 pm

how to calculate label values in an array

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

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

how to calculate label values in an array

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

Michael4771079
Posts: 0
Joined: Sat Jul 21, 2012 2:03 pm

how to calculate label values in an array

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

Image

Image

Im sure your sick of this issue :)

Return to “Issues”