Nested WHILE loops not acting as expected - Javascript / Google Apps Script

Posted by dthor on Stack Overflow See other posts from Stack Overflow or by dthor
Published on 2012-12-04T04:17:26Z Indexed on 2012/12/04 5:04 UTC
Read the original article Hit count: 210

I've got a function that isn't acting as intended. Before I continue, I'd like preface this with the fact that I normally program in Mathematica and have been tasked with porting over a Mathematica function (that I wrote) to JavaScript so that it can be used in a Google Docs spreadsheet. I have about 3 hours of JavaScript experience...

The entire (small) project is calculating the Gross Die per Wafer, given a wafer and die size (among other inputs). The part that isn't working is where I check to see if any corner of the die is outside of the effective radius, Reff.

The function takes a list of X and Y coordinates which, when combined, create the individual XY coord of the center of the die. That is then put into a separate function "maxDistance" that calculates the distance of each of the 4 corners and returns the max. This max value is checked against Reff. If the max is inside the radius, 1 is added to the die count.

// Take a list of X and Y values and calculate the Gross Die per Wafer
function CoordsToGDW(Reff,xSize,ySize,xCoords,yCoords) {

  // Initialize Variables
  var count = 0;

  // Nested loops create all the x,y coords of the die centers
  for (var i = 0; i < xCoords.length; i++) {
    for (var j = 0; j < yCoords.length, j++) {         
      // Add 1 to the die count if the distance is within the effective radius
      if (maxDistance(xCoords[i],yCoords[j],xSize,ySize) <= Reff) {count = count + 1}
    }
  }

  return count;
}

Here are some examples of what I'm getting:

xArray={-52.25, -42.75, -33.25, -23.75, -14.25, -4.75, 4.75, 14.25, 23.75, 33.25, 42.75, 52.25, 61.75}
yArray={-52.5, -45.5, -38.5, -31.5, -24.5, -17.5, -10.5, -3.5, 3.5, 10.5, 17.5, 24.5, 31.5, 38.5, 45.5, 52.5, 59.5}
CoordsToGDW(45,9.5,7.0,xArray,yArray)
  returns: 49 (should be 72)

xArray={-36, -28, -20, -12, -4, 4, 12, 20, 28, 36, 44}
yArray={-39, -33, -27, -21, -15, -9, -3, 3, 9, 15, 21, 27, 33, 39, 45}
CoordsToGDW(32.5,8,6,xArray,yArray)
  returns: 39 (should be 48)

I know that maxDistance() is returning the correct values. So, where's my simple mistake?

Also, please forgive me writing some things in Mathematica notation...

Edit #1: A little bit of formatting.

Edit #2: Per showi, I've changed WHILE loops to FOR loops and replaced <= with <. Still not the right answer. It did clean things up a bit though...

Edit #3: What I'm essentially trying to do is take [a,b] and [a,b,c] and return [[a,a],[a,b],[a,c],[b,a],[b,b],[b,c]]

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about mathematica