Code Lab

Modo entrevista. Temporizador de 7 min, sin pistas y sin autocompletado

Check the response

FácilErrors7 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.

Ejemplos

readUser('{"name":"Ann","age":30}')

Devuelve { name: 'Ann', age: 30 }

new ValidationError('Missing field: age').name

Devuelve 'ValidationError'

Fuente

Cargando el editor…
  1. EntradareadUser('{"name":"Ann","age":30}')
    Esperado{ name: 'Ann', age: 30 }
  2. EntradareadUser('{"name":"Ann"}')
    EsperadoLanza ValidationError 'Missing field: age'
  3. The thrown error is a ValidationError and an Error

Las pruebas ocultas se ejecutan al enviar