4 ms·
# Check if the number is a multiple of 3 and 5 if [[ $1 -eq 0 ]]; then echo "FizzBuzz" It knows what it wants, just not quite how to do it here.
by tedunangst 4y ago
# Check if the number is a multiple of 3 and 5
if [[ $1 -eq 0 ]]; then
echo "FizzBuzz"
It knows what it wants, just not quite how to do it here.
- nl 4y agoGood pickup! > In this code it incorrectly prints FizzBuzz when $1 is 0. Fix it # Function to solve the FizzBuzz problem fizzbuzz() { # Check if the number is greater than 100 if [[ $1 -gt 100 ]]; then # If so, return return fi # Check if the number is a multiple of 3 and 5 if [[ $(( $1 % 3 )) -eq 0 && $(( $1 % 5 )) -eq 0 ]]; then echo "FizzBuzz" # Check if the number is a multiple of 3 elif [[ $(( $1 % 3 )) -eq 0 ]]; then echo "Fizz" # Check if the number is a multiple of 5 elif [[ $(( $1 % 5 )) -eq 0 ]]; then echo "Buzz" # Otherwise, print the number else echo $1 fi # Increment the number and call the function recursively fizzbuzz $(( $1 + 1 )) } # Call the function to solve the FizzBuzz problem fizzbuzz 1