what am i doing wrong with this calculation?

308
2
04-21-2023 11:58 AM
EvanDarpini
New Contributor II

Its suppose to perform the square root of the sum of squares depending on how many inputs are selected
if(selected(${DBH}, '1'), (sqrt(pow(${DBH1}, 2))),

if(selected(${DBH}, '2'), (sqrt(pow(${DBH1}, 2) + pow(${DBH2}, 2))),

if(selected(${DBH}, '3'), (sqrt(pow(${DBH1}, 2) + pow(${DBH2}, 2) + pow(${DBH3}, 2))),

if(selected(${DBH}, '4'), (sqrt(pow(${DBH1}, 2) + pow(${DBH2}, 2) + pow(${DBH3}, 2) + pow(${DBH4}, 2))),

if(selected(${DBH}, '5'), (sqrt(pow(${DBH1}, 2) + pow(${DBH2}, 2) + pow(${DBH3}, 2) + pow(${DBH4}, 2) + pow(${DBH5}, 2))),

if(selected(${DBH}, '6'), (sqrt(pow(${DBH1}, 2) + pow(${DBH2}, 2) + pow(${DBH3}, 2) + pow(${DBH4}, 2) + pow(${DBH5}, 2) + pow(${DBH6}, 2))),

if(selected(${DBH}, '7'), (sqrt(pow(${DBH1}, 2) + pow(${DBH2}, 2) + pow(${DBH3}, 2) + pow(${DBH4}, 2) + pow(${DBH5}, 2) + pow(${DBH6}, 2) + pow(${DBH7}, 2))),

if(selected(${DBH}, '8'), (sqrt(pow(${DBH1}, 2) + pow(${DBH2}, 2) + pow(${DBH3}, 2) + pow(${DBH4}, 2) + pow(${DBH5}, 2) + pow(${DBH6}, 2) + pow(${DBH7}, 2) + pow(${DBH8}, 2))),

if (selected(${DBH}, '9'), (sqrt(pow(${DBH1}, 2) + pow(${DBH2}, 2) + pow(${DBH3}, 2) + pow(${DBH4}, 2) + pow(${DBH5}, 2) + pow(${DBH6}, 2) + pow(${DBH7}, 2) + pow(${DBH8}, 2) + pow(${DBH9}, 2))),

if(selected(${DBH}, '10'), (sqrt(pow(${DBH1}, 2) + pow(${DBH2}, 2) + pow(${DBH3}, 2) + pow(${DBH4}, 2) + pow(${DBH5}, 2) + pow(${DBH6}, 2) + pow(${DBH7}, 2) + pow(${DBH8}, 2) + pow(${DBH9}, 2) + pow(${DBH10}, 2))))))))))))

0 Kudos
2 Replies
IsmaelChivite
Esri Notable Contributor

Hi @EvanDarpini  I think a JavaScript function will be perfect for this. For example:

function squareRootOfSumOfSquares(nums) {
  let sumOfSquares = 0;
  let numsArray = nums.split(',');
  for (let i = 0; i < numsArray.length; i++) {
    sumOfSquares += numsArray[i] ** 2;
  }
  const squareRoot = Math.sqrt(sumOfSquares);
  return sumOfSquares;
}

 

The function takes a comma separated list of numbers, squares them all one by one and calculates the sum. Then it returns the square root of the sum.

You can generate a comma separated list of numbers in different ways. Below is an animation showing the test I did with all of this. I created three experiences to enter the data. One using a single text question, a series of questions and a repeat. Just for fun.

Squares.gif

 Attaching a the XLSForm illustrating the concept.  If you are not familiar with JS functions check out this blog

abureaux
MVP Regular Contributor

If JavaScript isn't an option, a repeat would be a good alternative. 

This is an example of what you seem to be using now:

abureaux_0-1682363322024.png

 

This is the same idea, but in a repeat:

abureaux_1-1682363397276.png

 

The repeat will work for as many instances of DBH as you need, all while taking considerably less nested IF statements (aka, none)!