FAQ: Enumerations - Implementing a Computed Property in an Enumeration

This community-built FAQ covers the “Implementing a Computed Property in an Enumeration” exercise from the lesson “Enumerations”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Intermediate Swift

FAQs on the exercise Implementing a Computed Property in an Enumeration

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

The first task is this:

Create a computed variable property in Vehicle called description that returns a String value.
Here are the description values for each case:

  • Airplane returns: "This is an airplane"
  • Boat returns: "This is a boat"
  • Truck returns: "This is a truck" if isFourWheelDrive is false
  • Truck returns: "This is a truck with four wheel drive" if isFourWheelDrive is true

The problem:

The code is only accepted if it is an exact copy of the suggested solution. It didn’t matter that my code had the correct logic for returning the truck’s description based on if it’s four wheel drive or not. I had to check the solution and replace my code with the solution before it was accepted.

In other words, this was a task of “guess the exact way someone else did this”.

2 Likes

What is the exact same solution, I’m unable to get it correct. My solution should work but does not match what they are asking for.

enum Vehicle {
case airplane
case boat
case truck(isFourWheelDrive: Bool)

// 1: Create computed value here
var description : String {
  switch self {
    case .airplane:
      return "This is an airplane"
    case .boat:
      return "This is a boat"
    case .truck(let isFourWheelDrive):
      if isFourWheelDrive {
        return "This is a truck with four wheel drive"
      } else {
        return "This is a truck"
      }
  }
}

}

2 Likes

I found a bug in View Solution mode. There are two the same parts of code, but one of them is not my code. I didn’t use ternary operator, so algorithm has taken the solution code and put it to both sections: my code and solution.

1 Like

I write code like this, but it can’t pass the first instruction’s code checking. The only code can pass needs to exactly the same as the solution.

enum Vehicle { case airplane case boat case truck(isFourWheelDrive: Bool) // 1: Create computed value here var description: String { switch self { case .airplane: return "This is an airplane" case .boat: return "This is a boat" case let .truck(isFourWheelDrive): if isFourWheelDrive { return "This is a truck with four wheel drive" } else { return "This is a truck" } } } }
1 Like

wrote exactly what you did and it was wrong. These last two lessons have been very confusing. perhaps they need to be re-written.

1 Like

Why does the .truck case require let, but none of the other cases do? Please elaborate in the hint.

1 Like

I’ve tried variations of both this (the original logic I wrote which worked two exercises ago) and the ternary in the hint and everything either throws an error, or else is still counted as wrong. How on earth do I pass this exercise/step?

I am unable to get the correct my solution after my logic code following:

enum Vehicle {
    case airplane
    case boat
    case truck(isFourWheelDrive: Bool)
    
    // 1: Create computed value here

    var description: String {
      switch self {
        case .airplane:
          return "This is an airplane"
        
        case .boat:
          return "This is a boat"
          
        case .truck(isFourWheelDrive: false):
          return "This is a truck"
          
        case .truck(isFourWheelDrive: true):
          return "This is a truck with four wheel drive"
      }
    }
}

So I got with NO errors in the console but I am still getting with the error’s message although my code works fine :

ERROR: Did you create a computed variable property with the right name and functionality?