Browse Source

refactor(division): modify solution to avoid having to exit

pull/1749/head
eslopfer 2 years ago
parent
commit
8bf61c6a45
  1. 12
      sh/tests/solutions/division.sh

12
sh/tests/solutions/division.sh

@ -4,25 +4,21 @@
if [ $# -ne 2 ] if [ $# -ne 2 ]
then then
echo "Error: two numbers must be provided" echo "Error: two numbers must be provided"
exit 0
fi
# # Check if the arguments are numeric # # Check if the arguments are numeric
if ! [[ $1 =~ ^-?[0-9]*\.?[0-9]+$ ]] || ! [[ $2 =~ ^-?[0-9]*\.?[0-9]+$ ]] elif ! [[ $1 =~ ^-?[0-9]*\.?[0-9]+$ ]] || ! [[ $2 =~ ^-?[0-9]*\.?[0-9]+$ ]]
then then
echo "Error: both arguments must be numeric" echo "Error: both arguments must be numeric"
exit 0
fi
# Check if the second argument is not 0 # Check if the second argument is not 0
if [ $(echo "$2 == 0" | bc) -eq 1 ] elif [ $(echo "$2 == 0" | bc) -eq 1 ]
then then
echo "Error: division by zero is not allowed" echo "Error: division by zero is not allowed"
exit 0
fi
# Divide the first argument by the second using bc # Divide the first argument by the second using bc
else
result=$(echo "$1 / $2" | bc ) result=$(echo "$1 / $2" | bc )
# Output the result # Output the result
echo $result echo $result
fi

Loading…
Cancel
Save