We want a function that either tells us:
There was an error with the following message: [message]
or
User [name] is [age] years old and loves [favouriteLang]
function displayErrorOrUser (id) {
var user = getUserRecord(id)
if (user.type === 'error') return "There was an error with the following message: " + user.message
return "User " + user.name + " is " + user.age + " years old and loves " + user.favouriteLang
}
console.log(displayErrorOrUser(1))
console.log(displayErrorOrUser(5))
function getUserRecord (id) {
return id > 2 ? { type: 'error', message: 'that was terrible' } : {
type: "success",
name: "Jigglypuff",
age: Infinity,
favouriteLang: "JavaScript",
dateRetrieved: new Date
}
}