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.
Catch up wih the Appery.io community on our forum. Here you'll find information on the lastest questions and issues Appery.io developers are discussing.
https://forum.appery.io/
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.
Hi Marina,
the variables are in localstorage, see second screenshot, when I run the app nothing shows for "price" and "extrasprice"
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.
So What do you think is the problem, am I running the code properly, on success of rest service?
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.
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
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.length; i++){
totalprice.push(+price["Price"] + (+extrasprice["Price"]))
};
}
localStorage.setItem("totalpriceArray", JSON.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.
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.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 = [];
{
totalprice.push(+price["Price"] + (+extrasprice["Price"]))
};
}
localStorage.setItem("totalpriceArray", JSON.stringify(totalprice));/code
Hi Michael,
Replace:pre{
totalprice.push(+price["Price"] + (+extrasprice["Price"]))
};
}/prewith:prefor (i = 0; i < price.length; i++){
totalprice.push(+price["Price"] + (+extrasprice["Price"]))
};/pre