mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-18 19:21:17 +00:00
first
This commit is contained in:
26
course/7-loops/exercises/2-omit_condition/code.go
Normal file
26
course/7-loops/exercises/2-omit_condition/code.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func maxMessages(thresh float64) int {
|
||||
// ?
|
||||
}
|
||||
|
||||
// don't edit below this line
|
||||
|
||||
func test(thresh float64) {
|
||||
fmt.Printf("Threshold: %.2f\n", thresh)
|
||||
max := maxMessages(thresh)
|
||||
fmt.Printf("Maximum messages that can be sent: = %v\n", max)
|
||||
fmt.Println("===============================================================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(10.00)
|
||||
test(20.00)
|
||||
test(30.00)
|
||||
test(40.00)
|
||||
test(50.00)
|
||||
}
|
||||
32
course/7-loops/exercises/2-omit_condition/complete.go
Normal file
32
course/7-loops/exercises/2-omit_condition/complete.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func maxMessages(thresh float64) int {
|
||||
totalCost := 0.0
|
||||
for i := 0; ; i++ {
|
||||
totalCost += 1 + (float64(i) * 0.01)
|
||||
if totalCost > thresh {
|
||||
return i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// don't edit below this line
|
||||
|
||||
func test(thresh float64) {
|
||||
fmt.Printf("Threshold: %.2f\n", thresh)
|
||||
max := maxMessages(thresh)
|
||||
fmt.Printf("Maximum messages that can be sent: = %v\n", max)
|
||||
fmt.Println("===============================================================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(10.00)
|
||||
test(20.00)
|
||||
test(30.00)
|
||||
test(40.00)
|
||||
test(50.00)
|
||||
}
|
||||
15
course/7-loops/exercises/2-omit_condition/expected.txt
Normal file
15
course/7-loops/exercises/2-omit_condition/expected.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
Threshold: 10.00
|
||||
Maximum messages that can be sent: = 9
|
||||
===============================================================
|
||||
Threshold: 20.00
|
||||
Maximum messages that can be sent: = 18
|
||||
===============================================================
|
||||
Threshold: 30.00
|
||||
Maximum messages that can be sent: = 26
|
||||
===============================================================
|
||||
Threshold: 40.00
|
||||
Maximum messages that can be sent: = 34
|
||||
===============================================================
|
||||
Threshold: 50.00
|
||||
Maximum messages that can be sent: = 41
|
||||
===============================================================
|
||||
24
course/7-loops/exercises/2-omit_condition/readme.md
Normal file
24
course/7-loops/exercises/2-omit_condition/readme.md
Normal file
@@ -0,0 +1,24 @@
|
||||
# Omitting conditions from a for loop in Go
|
||||
|
||||
Loops in Go can omit sections of a for loop. For example, the `CONDITION` (middle part) can be omitted which causes the loop to run forever.
|
||||
|
||||
```go
|
||||
for INITIAL; ; AFTER {
|
||||
// do something forever
|
||||
}
|
||||
```
|
||||
|
||||
## Assignment
|
||||
|
||||
Complete the `maxMessages` function. Given a cost threshold, it should calculate the maximum number of messages that can be sent.
|
||||
|
||||
Each message costs `1.0`, plus an additional fee. The fee structure is:
|
||||
|
||||
* 1st message: `1.0 + 0.00`
|
||||
* 2nd message: `1.0 + 0.01`
|
||||
* 3rd message: `1.0 + 0.02`
|
||||
* 4th message: `1.0 + 0.03`
|
||||
|
||||
## Browser freeze
|
||||
|
||||
If you lock up your browser by creating an infinite loop that isn't breaking, just click the `cancel` button.
|
||||
Reference in New Issue
Block a user