Note that there are some explanatory texts on larger screens.

plurals
  1. PONested WHILE loops not acting as expected - Javascript / Google Apps Script
    primarykey
    data
    text
    <p>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...</p> <p>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.</p> <p>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.</p> <pre><code>// 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 &lt; xCoords.length; i++) { for (var j = 0; j &lt; 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) &lt;= Reff) {count = count + 1} } } return count; } </code></pre> <p>Here are some examples of what I'm getting:</p> <pre><code>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) </code></pre> <p>I know that maxDistance() is returning the correct values. So, where's my simple mistake?</p> <p>Also, please forgive me writing some things in Mathematica notation...</p> <p>Edit #1: A little bit of formatting.</p> <p>Edit #2: Per showi, I've changed WHILE loops to FOR loops and replaced &lt;= with &lt;. Still not the right answer. It did clean things up a bit though...</p> <p>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]]</p> <p><strong>Edit #4:</strong></p> <p>So it turns out my nested loops <strong>are</strong> working correctly: when I remove the maxDistance function and replace it with 1 (so that 1 &lt;= Reff is always true), I find that the total number of loop executions is correct.</p> <p>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.</p> <p>Thanks for the help everyone!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload