Adding The Twist
Part of the Fundamentals section of Coddy's GDScript journey. Lesson 61 of 92.
Challenge
EasyKeep everything you wrote in the previous steps, and add a twist to the rules inside fizzbuzz():
A number that is not divisible by 3 or 7 but contains the digit 3 returns "Almost Fizz" instead of the number. For example, 13 and 23 become Almost Fizz, while 30 is still Fizz and 35 is still Buzz.
To check whether a string contains another string, use in: "3" in "13" is true. The number has to be turned into a string first, so the check is "3" in str(number).
For the input 14, the output is:
Welcome to FizzBuzz!
1
2
Fizz
4
5
Fizz
Buzz
8
Fizz
10
11
Fizz
Almost Fizz
BuzzTry it yourself
extends Node
func _ready():
print("Welcome to FizzBuzz!")
var limit := int(OS.read_string_from_stdin())
for i in range(1, limit + 1):
print(fizzbuzz(i))
func fizzbuzz(number: int) -> String:
var result := ""
if number % 3 == 0:
result += "Fizz"
if number % 7 == 0:
result += "Buzz"
if result == "":
result = str(number)
return result
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators10Functions
Declare a FunctionArgumentsReturnReturn TypesRecap - Sigma FunctionRecap - Validation FunctionDefault Values5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary OperatorPractice on your own: Online GDScript compiler