Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Solution: FizzBuzz in function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	for i:=1; i < 16; i++ {
		fmt.Println(fb(i))
	}
}

func fb(n int) string {
	resp := ""

	if n % 3 == 0 {
		resp += "Fizz"
	}
	if n % 5 == 0 {
		resp += "Buzz"
	}
	if resp == "" {
		resp = strconv.Itoa(n)
	}
	return resp
}
package main

import (
	"fmt"
	"strconv"
	"testing"
)

func TestRegular(t *testing.T) {
	for _, n := range [...]int{1, 2, 4} {
		expected := strconv.Itoa(n)
		actual := fb(n)
		if actual != expected {
			t.Error(fmt.Sprintf("Expected '%v', Actual '%v'", expected, actual))
		}

	}
}

func TestFizz(t *testing.T) {
	expected := "Fizz"
	for _, n := range [...]int{3, 6, 9, 12, 18} {
		actual := fb(n)
		if actual != expected {
			t.Error(fmt.Sprintf("Expected '%v', Actual '%v'", expected, actual))
		}

	}
}

func TestBuzz(t *testing.T) {
	expected := "Buzz"
	for _, n := range [...]int{5, 10, 20, 25} {
		actual := fb(n)
		if actual != expected {
			t.Error(fmt.Sprintf("Expected '%v', Actual '%v'", expected, actual))
		}

	}
}

func TestFizzBuzz(t *testing.T) {
	expected := "FizzBuzz"
	for _, n := range [...]int{15, 30, 60} {
		actual := fb(n)
		if actual != expected {
			t.Error(fmt.Sprintf("Expected '%v', Actual '%v'", expected, actual))
		}

	}
}