0

i am trying for the first time to implement OOP in javascript and i got stuck on a recursive function when i try to send an array of objects to this function. So, i have the "Pitic" class (pitic means midget in romanian) with some propreties:

function Pitic(piticID) {
this.id = piticID;
this.inaltime = null;
this.greutate = null;
this.genereazaGreutate();
this.genereazaInaltime();
}

I'm now generating some midgets and storing them in the public piticiCollection Array variable. The "genereazaGreutate" and "genereazaInaltime" are function to generate random values for the inaltime and greutate values.

var pitic = new Pitic(idPitic);
piticiCollection.push(pitic);

The problem appears when i try to send the array of midgets to a function because all i get is only the first item of the array.

So, before i call the function, i have piticiCollection array with 4 objects: midgets are safe and sound http://img443.imageshack.us/img443/484/yr4f.png

And as soon as i call the function with the piticiCollection as a parameter i loose 3 midgets! :( most of the midgets are gone http://img201.imageshack.us/img201/5808/7od5.png

p.s. please excuse me for my bad english..

[EDIT] Here is a fiddle of my full code: http://jsfiddle.net/WT7Ud/ I call the function on line 56 and as soon as the debugger hits line 60 i loose array items.

8
  • 3
    Where's the code where you're passing the array of objects to the function? That's what we need to see. How you create the array, how you pass it to the function and how you access it inside the function. Commented Oct 23, 2013 at 23:27
  • @fDruga Try passing an inline array and check what you get. determinaPerechiPosibile([1, 2, 3, 4]); Commented Oct 23, 2013 at 23:34
  • @jfriend00 You can see in screenShot1 the array filled with objects being passed to the function "determinaPerechiPosibile". In screenShot 2 you can see the function. I create the array using a for and adding Pitic objects with the code piticiCollection.push(pitic); Commented Oct 23, 2013 at 23:35
  • @plalx it's not the case, because i need the objects to be avalable in the functon Commented Oct 23, 2013 at 23:38
  • Array members don't just disappear. Is it just in the debugger that it shows 1 member, or is it doing that in the natural output of your code? Commented Oct 23, 2013 at 23:39

3 Answers 3

2

I have solved my problem by creating a copy of the array before using it in the function. Strange :(

function determinaPerechi(somePitici) {
var piticDeComparat, colectieDePiticiCopy;
colectieDePiticiCopy = somePitici;

for (var i = 1; i < colectieDePiticiCopy.length; i++) {
    var piticDeComparat2 = null;
    piticDeComparat = colectieDePiticiCopy[0];
    piticDeComparat2 = colectieDePiticiCopy[i];

    if (piticDeComparat.inaltime < piticDeComparat2.inaltime) {
        //Perechea poate fi prietena
    }
}
//colectieDePiticiCopy.splice(0, 1);
if (colectieDePiticiCopy.length == 0) {
    //alert("finish");
    return;
}
determinaPerechi(colectieDePiticiCopy);
//test(ttt);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your determinaPerechiPosibile is modifying the original array on this line:

colectieDePitici.splice(1, colectieDePitici.length);

In particular, it is removing all but the first element. You probably should be using slice to non-destructively extract the part of the array you want to recurse on.

1 Comment

splice is called after i check the integrity of my array. I commented that line and still experiencing the issue.
0

As Ted Hopp mentioned, the problem appears to be the line

colectieDePitici.splice(1, colectieDePitici.length);

in combination with this line:

determinaPerechiPosibile(colectieDePiticiCopy);

If those two lines are commented out, the array maintains its original length.

1 Comment

OK, the splice was not the problem. I have defined a new function called test and passed the array. It worked fine. I added the for and broked it again. I have written again the function and it worked.. Of course i had to modify the splice to splice(0,1) but i say this again... even before the code hit the splice function my array was broken. For me it remains a mistery and i call it a day for now.. Thanks for the replies though..

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.