Really stuck on Need for Speed! (C#)

I’m fairly new to Exercism, and to coding in general, but I’m about 30 exercises in to the C# track, and really liking it. However, I’m stuck on Need for Speed, and as it’s quite a fundamental Learning Exercise, there’s lots of other exercises hidden behind it. I don’t want to turn off the Learning Mode, as I think the sequence of topics is probably relevant, but I don’t know how to progress.

Here’s my code. If I include the lines defining Speed and BatteryDrain in the RaceTrack class, it passes Tasks 1-5, but fails on Task 6. Alternatively, if I comment them out (and the expression-bodied definition for TryFinishTrack) and use the (currently commented out) While loop, it just times out.

class RemoteControlCar
{
    // TODO: define the constructor for the 'RemoteControlCar' class

    public int Speed {get; set;}
    public int BatteryDrain {get; set;}
    public int driven = 0;
    public int battery = 100;
    
    public RemoteControlCar(int speed, int batteryDrain)
    {
        this.Speed = speed;
        this.BatteryDrain = batteryDrain;
        int driven = 0;
        int battery = 100;
    }
        
    public bool BatteryDrained()
    {
        bool drained = false;
        if (battery < BatteryDrain)
        {
            drained = true;
        }
        return drained;   
    }

    public int DistanceDriven() => driven;
    
    public void Drive()
    {
        if (battery >= BatteryDrain)
        {
            driven += Speed;
            battery -= BatteryDrain;
        }
               
    }

    public static RemoteControlCar Nitro()
    {
        var Nitro = new RemoteControlCar(50, 4);
        return Nitro;
    }
   
}

class RaceTrack
{
    // TODO: define the constructor for the 'RaceTrack' class

    int Speed = 1;
    int BatteryDrain = 1;
    
    public int Distance {get; set;}
        
    public RaceTrack(int distance)
    {
        this.Distance = distance;        
    }

    public bool TryFinishTrack(RemoteControlCar car)  => (Speed * (100/BatteryDrain) >= Distance) ? true : false;
    /*{
        bool empty = car.BatteryDrained();
        while (empty == false)
        {
            car.Drive();
        }
        int total = car.DistanceDriven();
        if (total >= Distance)
        {
            return true;
        }
        else return false;        
    }*/
    
}

(Sorry, I don’t know how to format this as a code block correctly yet, I will edit the post if/when I learn that!) Edit: learned this now :)

I know Exercism isn’t for beginners, and when I first tried it late last year, I couldn’t engage with even the Lasagna exercise, and was put off. I’ve learned quite a lot since then, and would like to continue, but need a pointer or two!

Best way is to use three backticks (the character next to z on most keyboards). Put the three backticks before and after and it turns into a codeblock :slight_smile: You can also add a language. E.g.

```csharp
while (empty == false) {}
```

becomes

while (empty == false) {}

(I updated your message for you. If you edit it, you can see the changes I made :))

That’s odd, the logic seems sound.

What error message do you get for task 6?

As I said, if I use the While loop, it just times out, error message as follows:
(Your tests timed out. This might mean that there was an issue in our infrastructure, but more likely it suggests that your code is running slowly. Is there an infinite loop or something similar?

Please check your code, and if nothing seems to be wrong, try running the tests again.).

If I use the other code, I fail one or more of the Task 6 tests, depending on the values I enter for Speed and BatteryDrain in RaceTrack (using 1 and 1 as above fails Test 15, but passes the others). I’m fairly sure I shouldn’t be entering these values manually, but this was the only way I could get to the Results screen to know if the other code was correct!

If empty is true at the start of the loop, then it remains true and the loop would never end.

I know, but my BatteryDrained() method in RemoteControlCar should return false until the car has called Drive() at least once?

No it should return true when the battery is greater or equal to the drain

Isaac’s answer is correct. Please carefully check his response

Thankyou, IsaacG and ErikSchierboom! I know now I needed to iterate the BatteryDrained() method as well, putting it in the loop results in all tasks passed.

2 Likes