It's worth mentioning (especially for the more novice JavaScripters reading this) that, as with most programming problems, there are lots of ways to correctly satisfy the requirements from the original question.
I know this isn't Code Review.SE, but you might benefit from some feedback on the example you provided, if you're interested:
- The variables
a
, b
, and c
are never used in any particularly useful way, and detract from the human readability of your code. You should probably remove their declarations entirely.
- You set the variable
L
three times consecutively. This doesn't do very much at all, considering also L
is overridden once F1()
is executed. You should probably remove these unnecessary assignments entirely.
- The HTML specification is clear that
ID
values should be unique across the entire document
space; anything else is invalid and can lead to headaches and undocumented behavior down the line, especially when JavaScript comes into play. In the simplified example, there really isn't any need to assign them ID
s at all (see solution).
- Relatively minor, but inline event handling (i.e., using the
onclick
attribute) is generally regarded as an outdated concept. Instead, use the addEventListener()
paradigm to make your code easier to interpret and debug.
- The function name
F1()
isn't particularly descriptive to what the function actually does when called. A future developer maintaining your code would have a less difficult time discerning what this method does if it was named something more descriptive, like validateForm()
, or even just validate()
.
To satisfy your requirements, you might look to write something more like what I've got below. In a nutshell, when the validate()
function is run, the following actions are taken:
- Instantiates
validNumbers
, an array of the valid inputs to be validated against he fields.
- Instantiates
inputFields
which evaluates to a NodeList
iterable which can be looped.
- Instantiates
feedbackElement
, a reference to your <p>
node in the DOM.
- Loops all the
inputFields
, and checks if the value of the current field
is in the validNumbers
array.
- If the validation is successful, the code removes the valid value from the
validNumbers
array (as it's already been used and can't be used again) and proceed to validate the next field.
- If the validation is unsuccessful, the code automatically sets the text of your feedback element to
"bad"
and breaks out of the validation loop.
- Once all three input fields have been validated, the code checks to see if there are any remaining elements of
validNumbers
left. If there are, not all elements were used and the feedback is once again set to "bad"
. Otherwise, the validation checks out and the feedback element's contents are set to "good"
.
function validate() {
var validNumbers = [1, 2, 3];
var inputFields = document.querySelectorAll("input[type='text']");
var feedbackElement = document.querySelector("p#feedBack");
for (field of inputFields) {
var fieldValue = parseInt(field.value);
if (validNumbers.includes(fieldValue)) {
// valid number & not encountered yet
validNumbers.splice(validNumbers.indexOf(fieldValue), 1);
} else {
// invalid number or already used
feedbackElement.innerText = "bad";
break; // break out of `for` loop, as there's already no possible way for the provided numbers to be "good"
}
}
if (validNumbers.length === 0) {
// all numbers were used
feedbackElement.innerText = "good";
} else {
// not all numbers were used
feedbackElement.innerText = "bad";
}
}
document.querySelector("button").addEventListener('click', function() {
validate();
});
<input type=text>
<input type=text>
<input type=text>
<button>check</button>
<p id="feedBack"> </p>