• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Better than a long list of IF statements?

Noob2022

New Coder
I have a checklist with about 30 items on it. Not all 30 items are required and sometimes, all 30 items may be required; pending situations determined by our employees. On each item, the results can either be Submitted or not Submitted.
Based on the requirements above, it gets messy if I'm going to check for each items (if it's required or not) and then to check results if it's Submitted or not using IF statements.

FN to check which are required, then loops through for the results? Just wondering if anyone has any better suggestions?

Thank you
 

Johna

Full-Stack Web Developer
Staff Team
Guardian
Use forEach()
JavaScript:
var checklist = ["required", "required", "not required", "required"];

checklist.forEach(function(element, index) {
  if (element == "required") {
    console.log("item " + ++index + " is required");
  } else {
    console.log("item " + ++index + " is not required");
  }
})
 
Last edited:

Noob2022

New Coder
Use forEach()
JavaScript:
var checklist = ["required", "required", "not required", "required"];

checklist.forEach(function(element, index) {
  if (element == "required") {
    console.log("item " + ++index + " is required");
  } else {
    console.log("item " + ++index + " is not required");
  }
})

So, do we check if it's Submitted (or not) within the console.log("item "...) block?
 
Last edited by a moderator:
Top