This is the second objective I’ve gotten stuck on and can’t figure out. I’ve logged into the discord channel and have asked twice for help. Not a very helpful community.
I am stuck on the following:
LEARN GO: LOOPS, ARRAYS, MAPS, AND STRUCTS – Stock Market – Step 5
It could be me but I think the wording isn’t clear. I had this same issue on the for loop objective.
Here’s my code:
package main
import (
“fmt”
“math/rand”
“time”
)
func randomNumberGen(min float32, max float32) float32 {
return min + (max-min)*rand.Float32()
}
// Task implementation goes here
type Stock struct {
name string
price float32
}
func (sto *Stock) updateStock() {
change := randomNumberGen(-10000, 10000)
sto.price = change
fmt.Println(sto.price)
}
func (sto *Stock) displayMarket(market string) {
for {
fmt.Println(market)
}
}
func main() {
rand.Seed(time.Now().UnixNano())
// Function calls go here
stockMarket := Stock{{“GOOG:”, 2313.50}, {“AAPL:”, 157.28}, {“FB:”, 203.77}, {“TWTR:”, 50.00}}
}
Hi there,
In your code you are not looping through the Stock slice.
I had the same problem, but managed to fix it here is my code below:
package main
import (
“fmt”
“math/rand”
“time”
)
func randomNumberGen(min float32, max float32) float32 {
return min + (max-min)*rand.Float32()
}
// Task implementation goes here
type Stock struct {
name string
price float32
}
func (stock *Stock) updateStock() {
change := randomNumberGen(-10000, 10000)
stock.price = change
}
func displayMarket(market Stock){
for i := 0; i <len(market); i++ {
fmt.Printf(“We have %s on the market for %f.\n”, market[i].name, market[i].price)
}
}
func main() {
rand.Seed(time.Now().UnixNano())
// Function calls go here
stockMarket := Stock{{“GOOG”, 2313.50}, {“AAPL”, 157.28}, {“FB”, 203.77}, {“TWTR”, 50.00}}
displayMarket(stockMarket)
stockMarket[2].updateStock()
displayMarket(stockMarket)
}
I hope this helps.
1 Like