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
interface RawUser { name: string; email: string; age: number; }
function solve(users: RawUser[]) {
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