Conflicting array contents when console.log'ed

When I console.log’ed an array from my code, I got this:
image

In the square brackets, the array contents were correct, but when I pressed the dropdown arrow, it showed incorrect contents. I did some testing and found that the dropdown menu thing printed the final contents of the array, but I did not change the contents of the array anywhere in my code.

Here’s the code if anyone is curious

let positions = [];
  let nums = [];
  
  let pushAndGetNumAt = (a) => {
    positions.push(a);
      
    let pushElem = convertFloat(equation, a, equation.length);
    if (typeof pushElem === "undefined")
      return undefined;
    nums.push(pushElem);
    console.log(nums)
    return pushElem.toString().length - 1;
  };
  
  for (let i = 0; i < equation.length; i++) {
    if (equation[i] === " ")
      continue;
    
    if (sharedFunctions.isdigit(equation[i])) {
      if (i > 0) {
        if (charComparison.validateOperation(equation[i - 1]) === 0 || (!sharedFunctions.isdigit(equation[i - 1]) && equation[i - 1] !== 'm' && equation[i - 1] !== '.')) {
          let ret = pushAndGetNumAt(i);
          if (typeof ret === "undefined")
            return undefined;
          i += ret;
        }
      }
      else {
        let ret = pushAndGetNumAt(i);
        if (typeof ret === "undefined")
          return undefined;
        i += ret;
      }
    }
    else {
      if (equation[i] === '-' && sharedFunctions.isdigit(equation[i + 1])) {
        if (i > 0) {
          if (charComparison.validateOperation(equation[i - 1]) === 0) {
            equation = equation.substring(0, i) + 'm' + equation.substring(i + 1);
            let ret = pushAndGetNumAt(i);
            if (typeof ret === "undefined")
              return undefined;
            i += ret;
          }
        }
        else {
          equation = equation.substring(0, i) + 'm' + equation.substring(i + 1);
          let ret = pushAndGetNumAt(i);
          if (typeof ret === "undefined")
            return undefined;
          i += ret;
        }
      }
      else if (charComparison.validateConstantChar(equation[i])) {
        if (i > 0) {
          if (!sharedFunctions.isdigit(equation[i - 1])) {
            let ret = pushAndGetNumAt(i);
            if (typeof ret === "undefined")
              return undefined;
            i += ret;
          }
        }
        else {
          let ret = pushAndGetNumAt(i);
          if (typeof ret === "undefined")
            return undefined;
          i += ret;
        }
      }
    }
  }
  positions.sort((a, b) => a - b); //Sort the positions in case there is some positions confusion
  let retMap = {
    "positions": positions,
    "numbers": nums
  };
  return retMap;

in this case, nums is the array being console.loged.

the scary thing about this code is that it can even be changed outside of this code. for example:

let result = thatAboveFunction();

result.numbers.pop();
result.numbers.push(999);

Oh wait I see what’s happening. The problem isn’t with the function code I posted, but with something else. Sorry

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.