Skip to content

Latest commit

 

History

History
46 lines (25 loc) · 797 Bytes

011.md

File metadata and controls

46 lines (25 loc) · 797 Bytes

Dealing with these errors

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
  }
}