Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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]]

Edit #4:

So it turns out my nested loops are working correctly: when I remove the maxDistance function and replace it with 1 (so that 1 <= Reff is always true), I find that the total number of loop executions is correct.

How I found it: I added some code that just prints out what the function is doing on each loop iteration and saw that X and Y were correct, but the maxDistance function was returning NaN or a very large value when X || Y were positive. I'll look into this function.

Thanks for the help everyone!

like image 585
dthor Avatar asked Mar 16 '26 03:03

dthor


1 Answers

If i'm not wrong you're taking one element more than the actually array size when you're doing

while (i <= xCoords.length) {

It should be

while(i < xCoords.length) {

but i prefer

for (var i = 0; i < xCoords.length; i++)

Dunno if it can help :)

And when you're doing var a = {10, 9, 8, 7} it's not an array but a Object, you probly mean var a = []; or it's mathematica thing :)

like image 57
showi Avatar answered Mar 19 '26 02:03

showi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!