0
votes

I am a beginner with apps script and am coding an inventory checker which compares the values in one array (quantities we have currently in stock) with another array of set threshold values which represent when each inventory item needs to be reordered.

If the quantity in stock (value of a given element in array 1) is less than or equal to the threshold value (value of same element in array 2), then i will have it do something useful later. for now i'm just sending each comparison result to a new boolean array for testing.

I know that the values in each of my arrays are correct and properly indexed for proper comparison before being sent to this function.

function compareArrays(quantitiesArray, thresholdsArray) {

  var boolArray = []; 

  quantitiesArray.forEach(function(quantity, i) {

    var threshold = thresholdsArray[i];

    if (quantity <= threshold) {
      boolArray.push('TRUE');
    }
    else {
      boolArray.push('FALSE');
    }
  })
  return boolArray;  
}

for some reason when my forEach loop compares the values of the two arrays, i am getting incorrect comparison results. for example, logs will show a comparison between quantity = 9000 and threshold = 11000 and it will spit out FALSE, which makes no sense.

i also tried a simple for loop and it spit back the exact same incorrect true/false values. i used a bunch of different Logger.log checks at every point to make sure the correct values from each array are being compared, and everything checks out.

1
Can you add the element in quantitiesArray and thresholdsArraySudhir Ojha
thanks for your feedback, sorry i don’t understand what you mean? if you see below i got it working by forcing the elements in the arrays to be numbers. but i’m still curious why apps script didn’t automatically cast them as the type they already were formatted as in the sheet, which was numbers.theunspeakablethings

1 Answers

0
votes

variables defined by var are by default string type,so 9000 and 11000 int your code are type of string,and comparison between 9000(string) and 11000(string) is false.you should convert the type to int before compare.