我正在尝试从 codewars ( link ) 中解决以下问题。但我不断收到以下错误:expected: "false" got: ""
.
所以不知何故我错过了参数或其他东西,或者我没有正确返回?
指示:
# Create a function isDivisible(n, x, y) that checks if a number n is
# divisible by two numbers x AND y. All inputs are positive, non-zero
# digits.
# Expected output:
isDivisible(12,2,6)--> true because 12 is divisible by 2 and 6
isDivisible(100,5,3)--> false because 100 is not divisible by 3
我的脚本:
#!/bin/bash
function isDivisible () {
if [ $(1 / 2) ] && [ $(1 / 2) ]
then
echo "true"
else
echo "false"
fi
}
使用模%
运算符并检查余数。如果是,0
则该数字是可整除的。
#!/bin/bash
isDivisible() {
if (($1 % $2 == 0 && $1 % $3 == 0)); then
echo "true"
else
echo "false"
fi
}
$ isDivisible 12 2 6
true
$ isDivisible 100 5 3
false
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句