Javascript If Then formula with multiple conditions

I am creating an Adobe Form.

I have the following fields:
IMPORTANCE (values: 1, 2, 3, 4, 5)
MISSING DATA (values: Yes, No)
INCORRECT DATA (values: Yes, No)
ISSUES.

If IMPORTANCE = 1, 2 or 3 and MISSING DATA and/or INCORRECT DATA = Yes, then ISSUES should display YES.

If IMPORTANCE = 1, 2 or 3 and MISSING DATA and INCORRECT DATA = No, then ISSUES should display NO.

If IMPORTANCE = 4 or 5 and MISSING DATA = Yes, then ISSUES should display YES. (regardless of INCORRECT DATA value)

If IMPORTANCE = 4 or 5 and MISSING DATA and INCORRECT DATA = No, then ISSUES should display No.

Any help is greatly appreciated. I’m a beginner :slight_smile:

Can you show us what you’ve tried? This is a learning platform.

It’s outside the course material, so what the heck…

I came up with this function that will return a boolean:

const areIssuesToDisplay = (importance, hasMissingData, hasIncorrectData) => {
  if (importance <= 3) {
    return hasMissingData && hasIncorrectData;
  }
  
  return hasMissingData;
}

I think that would work - we could set up a bunch of test cases I guess. The only issue is that you don’t say what to do if IMPORTANCE is 4 or 5, MISSING DATA is false and INCORRECT DATA is true.

Thanks Kevin.

The only issue is that you don’t say what to do if IMPORTANCE is 4 or 5, MISSING DATA is false and INCORRECT DATA is true.

Then ISSUES should display No.

I tried the following, but I think I’m way off…

var f1 = this.getField(“Importance”);
var f2 = this.getField(“Missing Data”);
var f3 = this.getField(“Incorrect Data”);
var f4 = this.getField(“Issues”);

if ((f1=“1”||“2”||“3”) && (f2=“Yes”))
{
textContent = ‘Yes’;
}

if ((f1=“1”||“2”||“3”) && (f3=“Yes”))
{
textContent = ‘Yes’;
}

if ((f1=“4”||“5”) && (f2=“Yes”))
{
textContent = ‘Yes’;
}

else

{
textContent = ‘No’;
}

This isn’t a valid way to check is a variable is one of multiple values.

1 Like

My logic was a little off - this is what I have now:

const areIssuesToDisplay = (importance, hasMissingData, hasIncorrectData) => {
  if (+importance <= 3) {
    return hasMissingData || hasIncorrectData;
  }
  
  return hasMissingData;
}


const testMatrix = [
  { importance: '1', hasMissingData: false, hasIncorrectData: false, expected: false },
  { importance: '1', hasMissingData: true,  hasIncorrectData: false, expected: true },
  { importance: '1', hasMissingData: false, hasIncorrectData: true,  expected: true },
  { importance: '1', hasMissingData: true,  hasIncorrectData: true,  expected: true },
  { importance: '2', hasMissingData: false, hasIncorrectData: false, expected: false },
  { importance: '2', hasMissingData: true,  hasIncorrectData: false, expected: true },
  { importance: '2', hasMissingData: false, hasIncorrectData: true,  expected: true },
  { importance: '2', hasMissingData: true,  hasIncorrectData: true,  expected: true },
  { importance: '3', hasMissingData: false, hasIncorrectData: false, expected: false },
  { importance: '3', hasMissingData: true,  hasIncorrectData: false, expected: true },
  { importance: '3', hasMissingData: false, hasIncorrectData: true,  expected: true },
  { importance: '3', hasMissingData: true,  hasIncorrectData: true,  expected: true },
  { importance: '4', hasMissingData: false, hasIncorrectData: false, expected: false },
  { importance: '4', hasMissingData: true,  hasIncorrectData: false, expected: true },
  { importance: '4', hasMissingData: false, hasIncorrectData: true,  expected: false },
  { importance: '4', hasMissingData: true,  hasIncorrectData: true,  expected: true },
  { importance: '5', hasMissingData: false, hasIncorrectData: false, expected: false },
  { importance: '5', hasMissingData: true,  hasIncorrectData: false, expected: true },
  { importance: '5', hasMissingData: false, hasIncorrectData: true,  expected: false },
  { importance: '5', hasMissingData: true,  hasIncorrectData: true,  expected: true },
];

console.clear();
testMatrix.forEach(({ importance, hasMissingData, hasIncorrectData, expected }) => {
  const actual = areIssuesToDisplay(importance, hasMissingData, hasIncorrectData);
  const hasPassed = actual === expected;
  console.log(`${hasPassed ? 'PASS' : 'FAIL'} - ${importance}, ${hasMissingData}, ${hasMissingData} => ${actual}`);
});

That has a matrix for testing it. There is a pen here if you want to mess around with it.

I used the JS standard for variable naming. And we don’t normally store strings like “yes” and “no” - those would probably be boolean - but that is easy to fix if you need to.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.