- 2

Golang project 38

Hi, please help me with the project 38 (match results). I have tried to fulfill the task with different code variants, but it seems i miss something. It says that i should append the array with the last result and then calculate the sum of its elements. I have tried to add the last result, but then the output is 20 (it requires 22) as it is a draw, so I've decided that maybe it should be a win, so I appended the first element and the output is correct is 22. Still it says that only the 1st case is correct and other doesn't display requires values package main import "fmt" func main() { results := []string{"w", "l", "w", "d", "w", "l", "l", "l", "d", "d", "w", "l", "w", "d"} var a int //results = append (results, results[(len(results)-1)]) results = append (results, results[0]) for _, v := range results { switch { case v == "w": a += 3 case v == "l": a += 0 case v == "d": a += 1 } } fmt.Println(a) }

8th May 2021, 8:52 PM
Leonid
Leonid - avatar
3 odpowiedzi
+ 5
You need to scan in a value from user input and add it to the results array. This is wrong: results = append (results, results[0]) This is right: lastMatchResult := "" fmt.Scanln(&lastMatchResult) results = append(results, lastMatchResult) The only reason test case 1 passed for you is because the user input for that case coincidentally was "w" which is the same as results[0]. On a side note, you can remove your "l" case since it does nothing. Adding 0 to an int is accomplishing nothing.
9th May 2021, 12:38 PM
Josh Greig
Josh Greig - avatar
0
package main import "fmt" func main() { results := []string{"w", "l", "w", "d", "w", "l", "l", "l", "d", "d", "w", "l", "w", "d"} var a int lastMatchResult := "" fmt.Scanln(&lastMatchResult) results = append(results, lastMatchResult) for _, v := range results { switch { case v == "w": a += 3 case v == "l": a += 0 case v == "d": a += 1 } } fmt.Println(a) }
17th Sep 2021, 5:50 PM
Mostafa Alafif
Mostafa Alafif - avatar