1

Apologies if I use the wrong semantics here, but I have a function that is going to eventually be embedded in an application, specifically FormAssembly. They allow for JS functions to be defined and then called within a page. So I drummed up the following function and unsurprisingly, it's been returning errors:

<script>
function getFormId(program) {
    if (['a0s1M00000GrLrY', 'a0s1M00000GrLra', 'a0s1M00000GrLre', 'a0s1M00000GrLsl', 'a0s1M00000GrLtA', 'a0s1M00000GrLtC', 'a0s1M00000GrLtD', 'a0s1M00000GrLtE', 'a0s1M00000GrLtF'].includes(program)) {
        return '76';
    } else if (['1', '2', '3', '4', '5', '6', '7', '8', '9'].includes(program)) {
        return '75';
    } else {
        return '74';
    }
}
</script>

So I was trying to tweak it to test on a fiddle site like https://jsfiddle.net/ but am not sure how to adjust the code to define the function and then call it. I tried pasting the following into the JS section of the fiddle and running it, but nothing gets outputted:

function getFormId(program) {
    if (['a0s1M00000GrLrY', 'a0s1M00000GrLra', 'a0s1M00000GrLre', 'a0s1M00000GrLsl', 'a0s1M00000GrLtA', 'a0s1M00000GrLtC', 'a0s1M00000GrLtD', 'a0s1M00000GrLtE', 'a0s1M00000GrLtF'].includes(program)) {
        return '76';
    } else if (['1', '2', '3', '4', '5', '6', '7', '8', '9'].includes(program)) {
        return '75';
    } else {
        return '74';
    }
}

getFormId('2')

If I could get help at least verifying the 'base' code or function definition is correct, that would be helpful. I'd also appreciate any help in modifying it to test in fiddle.

2 Answers 2

2

You're currently calling the function but not doing anything with its return value.

You can log it to the web console:

console.log(getFormId('2'));

You'll need to open the browser's devtools (usually Ctrl+Shift+I and/or F12) and switch to the Console tab to see that.

Alternately, you can write it to an element in the DOM:

function log(msg) {
    var p = document.createElement("pre");
    p.appendChild(document.createTextNode(msg));
    document.body.appendChild(p);
}

and then

log(getFormId('2'));
Sign up to request clarification or add additional context in comments.

Comments

0

On a side note, you could actually simplify this logic by mapping the programs to the form IDs in an object structure and simply retrieving the form ID that is mapped to the program value that is passed in:

const oProgramMapping = {
  'default': '74',
  'a0s1M00000GrLrY': '76',
  'a0s1M00000GrLra': '76',
  'a0s1M00000GrLre': '76',
  'a0s1M00000GrLsl': '76',
  'a0s1M00000GrLtA': '76',
  'a0s1M00000GrLtC': '76',
  'a0s1M00000GrLtD': '76',
  'a0s1M00000GrLtE': '76',
  'a0s1M00000GrLtF': '76',
  '1': '75',
  '2': '75',
  '3': '75',
  '4': '75',
  '5': '75',
  '6': '75',
  '7': '75',
  '8': '75',
  '9': '75'
};

function getFormId(program) {
    return oProgramMapping[program] || oProgramMapping.default;
}

console.log('"a0s1M00000GrLrY" ID is: ', getFormId('a0s1M00000GrLrY'));
console.log('"6" ID is: ', getFormId('6'));
console.log('"XYZ" ID is: ', getFormId('XYZ'), ' (should default to "74")');

There are a number of benefits to this kind of approach, compared to embedding the values in the code . . . not only does this approach simplify the code by replacing logic with a relational data structure, it allows for the possibility of removing hardcoded data values from the code all together (i.e., assign the oProgramMapping variable value from an external data file import, service call, etc.), meaning that the data can be maintained without a change to the code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.