0

Ok, so I I'm having this strange behaviour that I cannot explain. Look at the following:

$("#completeData").on("click", function() {
  var toUpdate = {};
  var toUpdateCount = 0;
  var ratios = {};
  This.calculateGradePerSize();
  //1) Select all sizes that are equal to NA or are Equal to 0 (means its a new one)
  $.each(This.logements, function(key, l) {
    if (l.sizeMyId === "NA" || l.sizeMyId === 0) {
      toUpdate[l.rueNum] = l;
      toUpdateCount++;
    } else { //else init the ratios because it means they are actually present
      /**
         //My problem is this variable, 
         I want it to be equal to an empty object
         But for reasons I cannot seem to understand, 
         it takes in account the latter modification in the code 
         that happens to this variables
      */
      ratios[l.sizeMyId] = {};
    }
  });

  console.log(toUpdate);
  console.log(ratios);
  console.log(This.sizeRatio);

  //2) Calculate Ratios and build the ratios function of the toUpdate
  $.each(This.sizeRatio, function(sizeMyId, count) {
    if (sizeMyId !== "NA" && sizeMyId != 0) {
      console.log("COUNT SIZE: " + count + " COUNT LOGEMENT: " + This.countLogement + " toUpdateCount: " + toUpdateCount + " SizeMyId: " + sizeMyId);
      console.log("Calculation: " + count / This.countLogement * toUpdateCount);
      ratios[sizeMyId].count = Math.ceil(count / This.countLogement * toUpdateCount);
      console.log("Calculation WITH CEIL: " + Math.ceil(count / This.countLogement * toUpdateCount));
      ratios[sizeMyId].grade = This.sizeGrade[sizeMyId];
      ratios[sizeMyId].sizeMyId = sizeMyId;
    }
  });

  console.log(ratios);
});

As explained in the multiline comment, my problem is the ratio variable. I tried declaring the variable without var prefix, so that JS doesn't know its existence but still, I want it to be empty object. In fact, the problem has stronger roots than simply that, I cannot update it. Each change I make to the ratios var are not registered, but I wanna start with the beginning how can I make sure that this variable is empty at the beginning of the function.

2
  • It seems you are treating ratios as both an object and an array ratios[sizeMyId].sizeMyId; and ratios = {}; - they are not the same. Commented May 21, 2016 at 23:43
  • Ya but there is not other way to dynamically call an element of object without using the [] notation. I can't really do ratios.DYNAMIC_PROPERTY, otherwise it will think its a new one. Commented May 21, 2016 at 23:47

1 Answer 1

1

I don't know if this question is really worth. Thinking about deleting it. My bug was that the count variable in the each function as well as the ratio definition were the same hence not registering.

As for the variable not being an empty one at function start. It simply how the JS engine works. If there is something not working, more likely than not, there is something wrong in your code.

$.each(This.sizeRatio, function (sizeMyId, count) {
  if (sizeMyId !== "NA" && sizeMyId != 0) {
    console.log("COUNT SIZE: " + count + " COUNT LOGEMENT: " + This.countLogement + " toUpdateCount: " + toUpdateCount + " SizeMyId: " + sizeMyId);
    console.log("Calculation: " + count / This.countLogement * toUpdateCount);   
    //HERE ratios[sizeMyId].count IS THE SAME than the anonymous function.
    ratios[sizeMyId].count = Math.ceil(count / This.countLogement * toUpdateCount); 
    console.log("Calculation WITH CEIL: " + Math.ceil(count / This.countLogement * toUpdateCount));
    ratios[sizeMyId].grade = This.sizeGrade[sizeMyId];
    ratios[sizeMyId].sizeMyId = sizeMyId;
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Nah man keep it. Show anybody who searches up your title that even the greatest programmers sometimes have "a-doy" moments.

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.