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!