0

Hello i am not sure why those empty elements still there even i clear (or clear function does not work guess). Might you guys have a look on this. I am getting this array after add another value into same array.I am asking whats is happening behind not just solving code thank you

enter image description here

this is function when ever i click button it adds 4 into DVALUE array.

if("q"+idcount+"d" == this.id){
    DVALUE[dcount++] = 4;
//  alert("D ARRAY"+DVALUE.toString());
    }

And this is when ever i click revert button it will remove last added number

  if ("d" === qwer) {
    // alert(""+DVALUE.toString());
    DVALUE.pop();
    cleararrayD(); // also calling this function to remove empty elements when ever this if occurs
}

And this is cleararrayD Function

function cleararrayD() {
    lens = DVALUE.length, i;
    for (i = 0; i < lens; i++) DVALUE[i] && DVALUE.push(DVALUE[i]); // copy non-empty values to the end of the array
    DVALUE.splice(0, lens);
  }

2 Answers 2

1

I am asking whats is happening behind not just solving code

The .length of DVALUE array does not change at cleararrayD() function call as an element is .push()ed to DVALUE array for each index of DVALUE before .splice() is called with original array .length at second parameter, removing the preceding elements to the elements .push()ed to the array.

Sign up to request clarification or add additional context in comments.

7 Comments

What is input and expected result?
input is just numbers and result is little bit complicated i am getting all those numbers by index there should be 0-11 index, all index has number that is why i need to clear empty elements otherwise i get undefined.
What do you mean by "empty" elements? Can you create a stacksnippets of jsfiddle jsfiddle.net to demonstrate?
[jsfiddle.net/Munkhdelger/5b6xLpfs/12/](jsfiddle.net) here you go as you can see click 4 button after that remove it and do it again you will see empty indexes
guess i almost what i done wrong i forget to minus index counter of every array DVALUE[Dcount++]= 4; if am i right i need to code this dcount-- in remove button ?
|
1

Okey i got answer for my question. As you can see it clears array but i put element at wrong indexes. Everytime i click button Dcount++ adding indexes so i just doing this Dcount-- in my removing functions

DVALUE[dcount++] = 4;

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.