Mode entretien. Minuteur de 7 min, sans indices ni autocomplétion
Check the response
FacileErrors7 min
Write a class ValidationError that extends Error and has the name 'ValidationError'. Then write a function readUser(json). It parses a JSON string and returns the user object. If the object has no name or no age field, throw a ValidationError with the message 'Missing field: name' or 'Missing field: age'. If the string is not JSON, the SyntaxError from JSON.parse goes on as it is.
Exemples
readUser('{"name":"Ann","age":30}')Renvoie { name: 'Ann', age: 30 }
new ValidationError('Missing field: age').nameRenvoie 'ValidationError'
Chargement de l'éditeur...
- Entrée
readUser('{"name":"Ann","age":30}')Attendu{ name: 'Ann', age: 30 } - Entrée
readUser('{"name":"Ann"}')AttenduLève ValidationError 'Missing field: age' - The thrown error is a ValidationError and an Error
Les tests cachés se lancent à la soumission