0

I have a piece of Javascript code that assigns string of values to a string array. Unfortunately if I try to add more than one string to the array, my UI simulator(which runs on JS code) closes unexpectedly. I have tried debugging but I cannot find anything. I am attaching that piece of code where the issue is. may be you guys could find some flaw? On the pop up button click the values I selcted on the UI should get stored in the array and I have a corressponding variable on the server side to handle this string array.

_popupButtonClick: function (button) {
                var solutions = this._stateModel.get('solutionName');
                var i;
                var solutionsLength = solutions.length;
                var selectedSolution = [solutionsLength];


                this.clearPopupTimer();
                if (button.position === StatusViewModel.ResponseType.Ok) {

                    for(i=0;i<solutionsLength;i++)
                    {
                        if(this._list.listItems[i].selected)
                        {
                            selectedSolution[i] = this._list.listItems[i].options.value;
                        }
                    }

                    this._stateModel.save({
                        selectedsolutions: selectedSolution,
                        viewResponse: StatusViewModel.ResponseType.Ok
                    });
                } else {
                    this._stateModel.save({
                        viewResponse: StatusViewModel.ResponseType.Cancel
                    });
                }

            }
2
  • StatusViewModel is it a javascript object? if not just make it as "OK" and "CANCEL" Commented May 20, 2016 at 5:20
  • StatusViewModel is used to handle the button presses. I have a corresponding mapping on the server side also. That is working fine. The problem is in the for loop and if condition is what i am thinking Commented May 20, 2016 at 5:22

3 Answers 3

1

Change

var selectedSolution = [solutionsLength];

to

var selectedSolution = [];

This makes your array have an extra item that might be causing a crash.

Also,

you have an

if(this._list.listItems[i].selected)
{
    selectedSolution[i] = this._list.listItems[i].options.value;
} 

But no corresponding else, so your array has undefined values for i which are not entering the if.

Maybe adding an empty string might solve it:

if(this._list.listItems[i].selected)
{
    selectedSolution[i] = this._list.listItems[i].options.value;
} 
else
{
    selectedSolution[i] = "";
}
Sign up to request clarification or add additional context in comments.

Comments

1

The code is looking fine but there seems to be a piece of code which can cause error. For example, you are assigning var selectedSolution = [solutionsLength]; and for example solutionsLength is 5 then your loop runs for 5 times

for(i=0;i<solutionsLength;i++) // runs for 5 times
{
    if(this._list.listItems[i].selected)
    {
        // but selectedSolution = [5]; which is on 0th index and from 1st to 4th index it is undefined
        selectedSolution[i] = this._list.listItems[i].options.value;
    }
}

So you can try to use push() like

 selectedSolution.push(this._list.listItems[i].options.value);

and on initialization change it like,

 var selectedSolution = [];

Hopefully this will solve your problem.

1 Comment

Thanks a ton for replying in such quick time. Sure, i ll try your approach and get back to you if it works (even if it doesn't work :p)
1
var selectedSolution = [solutionsLength]; 

keeps the value in the selectedSolution variable.

    var selectedSolution = [3];
    selectedSolution[0] gives the values as 3

So make it simple

var selectedSolution = [];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.