Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Good 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


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: