I’m working on the Chessboard exercise, and I’ve gotten all the methods working except for CountInRank.
// CountInRank returns how many squares are occupied in the chessboard,
// within the given rank.
func CountInRank(cb Chessboard, rank int) int {
var num_occupied int = 0
if rank < 1 || rank > 8 {
return 0
} else {
for i, _ := range cb {
if cb[i][rank] == true {
num_occupied += 1
}
}
return num_occupied
}
}
This seems to me like it should hit all the files of the rank, and it seems to, but the returned counts are not matching the expected results.
Would this solution work to count, say, the first/left most rank?
I don’t see why it wouldn’t; I am only allowing ranks between 1 and 8 to be calculated through iteration, per the test case instructions.
What index gives the first rank?
Per the test cases, it looks like ranks 1-8 should have nonzero results, so 1 should give the first rank.
What number needs to be used for rank
to get the left most rank? I’m looking for a single number as the answer 
The left most rank would be 1.
So we’d do something like this to print the first element (5) of the array? You may want to check what this code prints.
func main() {
data := []int{5, 10, 15, 20}
fmt.Println(data[1])
}
Ah, I figured it out. While the left most rank is 1, the left most array index is 0, so I had to do a rank - 1 during the iteration. Thank you for the help!
1 Like