Need for speed exercise

What is wrong here?

package speed

// TODO: define the ‘Car’ type struct

type Car struct {
battery int
batteryDrain int
speed int
distance int
}

// NewCar creates a new remote controlled car with full battery and given specifications.
func NewCar(speed, batteryDrain int) Car {
return Car{
battery: 100,
batteryDrain: batteryDrain,
speed: speed,
distance: 0,
}
}

  1. If you use codeblocks, the code is much easier to read.
  2. You should run the tests. The tests will tell you what’s wrong. If you’d like help interpreting the test output, please share the test output (using a codeblock and not a screenshot).

here are the errors in a code block (I hope)

typ# speed [speed.test]
./need_for_speed.go:25:29: undefined: Track
./need_for_speed.go:36:31: undefined: Track
./need_for_speed_test.go:41:12: undefined: Track
./need_for_speed_test.go:42:12: undefined: Track
./need_for_speed_test.go:46:11: undefined: Track
./need_for_speed_test.go:49:14: undefined: Track
./need_for_speed_test.go:55:11: undefined: Track
./need_for_speed_test.go:58:14: undefined: Track
./need_for_speed_test.go:188:12: undefined: Track
./need_for_speed_test.go:199:11: undefined: Track
./need_for_speed_test.go:199:11: too many errors
FAIL	speed [build failed]
'/usr/local/go/bin/go test --short --json .' returned exit code 1: exit status 1e or paste code here

the errors are found on lines I haven’t completed yet so are expected… not sure what I am doing wrong with the first exercise :frowning:

Based on the errors, I would say you need to define a Track object to compile the code :slight_smile:

1 Like

This was the initial code that was given to you: did you delete it?

package speed

// TODO: define the 'Car' type struct

// NewCar creates a new remote controlled car with full battery and given specifications.
func NewCar(speed, batteryDrain int) Car {
	panic("Please implement the NewCar function")
}

// TODO: define the 'Track' type struct

// NewTrack creates a new track
func NewTrack(distance int) Track {
	panic("Please implement the NewTrack function")
}

// Drive drives the car one time. If there is not enough battery to drive one more time,
// the car will not move.
func Drive(car Car) Car {
	panic("Please implement the Drive function")
}

// CanFinish checks if a car is able to finish a certain track.
func CanFinish(car Car, track Track) bool {
	panic("Please implement the CanFinish function")
}

Remember to read the entire exercise and the comments, and if you are ever left wondering why there are so many errors, check how the tests are written and what they are requiring to be executed, the test says it all, you need the Track, but surely now you did it. I always recommend you review this before, it can be of great help and you will learn the importance of the tests!

2 Likes

this was it :woman_facepalming: thank you!

bubblyscar76, maybe for Truck something similar to Car, like this:

type Car struct {
battery int
speed int
batteryDrain int
distance int
}

func newCar(speed int, batteryDrain int) *Car {
c := Car{speed: speed}
c.batteryDrain = batteryDrain
c.battery = 100
c.distance = 0
return &c
}

type Track struct {
distance int
//name string
}

func newTrack(dist int) *Track {
t := Track{}
t.distance = dist
//t.name = name
return &t
}

They posted about 1.5 months ago that they got it working :slight_smile: I suspect they’re no longer working on this exercise anymore!