Problems › JavaScript › validation
Which required fields are missing
A form submission arrives as a bag of strings and the server checks it before touching the database.
- A field is missing when its key is absent, or its value is empty or only whitespace.
- Report them in the order the requirements were given, not the order the payload happens to be in.
- Nothing required means nothing missing.
missingFields(payload: map<string, string>, required: list<string>) → list<string>
Where you start
function missingFields(payload, required) {
}
Worked examples
| Call | Result |
|---|---|
missingFields({"name":"Ada","email":""}, ["name","email","phone"]) | ["email","phone"] |
missingFields({"name":" "}, ["name"]) | ["name"] |
missingFields({"name":"Ada"}, ["name"]) | [] |
missingFields({}, []) | [] |
Hint
Walk the requirements, not the payload — that is what fixes the output order.
Reference solution in JavaScript
function missingFields(payload, required) {
const result = [];
for (const key of required) {
const v = payload[key];
if (v === undefined || v.trim() === '') result.push(key);
}
return result;
}