EasyPythonJavaScriptTypeScript
Extract Function
Clean CodeFunctionsRefactoring
The function below does too many things at once — it validates, transforms and formats all in one block.
Refactor solve(users) by extracting helper functions so each function does exactly one thing. The output must be identical.
// dirty
function solve(users) {
const result = [];
for (const u of users) {
if (u.age >= 18 && u.email.includes('@') && u.name.trim().length > 0) {
result.push({ name: u.name.trim(), email: u.email.toLowerCase(), adult: true });
}
}
return result;
}Your extracted version should have at minimum: isValid(user), formatUser(user), and the main solve(users) that composes them.
Sample tests
Test #1Name is trimmed and email is lowercased
Input: [[{"age":30,"name":" Bob ","email":"BOB@EXAMPLE.COM"}]]
Output: [{"name":"Bob","adult":true,"email":"bob@example.com"}]
Test #2Under 18 is filtered out
Input: [[{"age":16,"name":"Teen","email":"teen@x.com"}]]
Output: []
Test #3Valid adult user passes through
Input: [[{"age":25,"name":"Alice","email":"alice@example.com"}]]
Output: [{"name":"Alice","adult":true,"email":"alice@example.com"}]