Modular FizzBuzz Test
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © PrismBot

//@version=5
indicator("Modular FizzBuzz Test", overlay = true)

// Inspired by having watched the Tom Scott video on FizzBuzz,
// this indicator was my attempt at doing FizzBuzz in Tradingview,
// in a simple and modular way. Mostly for fun but also to,
// even with my experience, learn something new and find out
// more about my own habits with repeating myself in code.
// 
// The goal is to show the simplest path to plotting Fizz or Buzz
// or FizzBuzz if the bar index of a given bar is equal to some
// multiples or is a multiple of both numbers.
//  
// We check for this using the modulo to see if a bar_index value has
// a remainder. If there is no remainder, output some text string.
//
// I tried to make this as modular as possible so it would be very 
// easy to add more lines and checks for "Fuzz" or "Bizz", etc.
// and avoid as much repetition as possible through use of functions.

bar             = bar_index

fizz_input      = input.int(3, title = "Fizz", group = "FizzBuzz")
buzz_input      = input.int(5, title = "Buzz", group = "FizzBuzz")
//fuzz_input      = input.int(7, title = "Fuzz", group = "FizzBuzz")

// define strings

fizz         = "Fizz"
buzz         = "Buzz"
//fuzz         = "Fuzz"

fb_calc(x,y) => bar % x == 0 ? y : na // calculates the remainder and if no remainder, output some string.

// contatenate all strings together in a function from the fb_calc()
fb_check() =>
    fb_calc(fizz_input,fizz) +
     fb_calc(buzz_input,buzz)// +
//     fb_calc(fuzz_input,fuzz)

output = str.tostring(fb_check())

if output != na // plot the text as long as some text exists
    label.new(x = bar_index, y = high, yloc = yloc.abovebar, text = output, textcolor = color.new(color.gray, 0), color = color.new(color.black, 100))

plotshape(str.contains(output, buzz), style = shape.circle, color = color.new(color.green,0), location = location.abovebar)
plotshape(str.contains(output, fizz), style = shape.cross, color = color.new(color.red,0), location = location.abovebar)
//plotshape(shape.star, color.new(color.blue, (output == fuzz ? 0 : 100)), text = output, location = location.abovebar)

