For an assignment I have to write a function that prints the number of even numbers when provided with a sequence of numbers.
I used the piece of code I used for a previous assignment (to print 1
when a number was even and 0
when the number was odd)
My problem now is that my function keeps printing 0
.
What am I doing wrong?
Here's my script:
#!/usr/bin/bash
# File: nevens.sh
# Write a function called nevens which prints the number of even numbers when provided with a sequence of numbers.
# Check: input nevens 42 6 7 9 33 = output 2
function nevens {
local sum=0
for element in $@
do
let evencheck=$(( $# % 2 ))
if [[ $evencheck -eq 0 ]]
then
let sum=$sum+1
fi
done
echo $sum
}