Swift - Write program to sum up all the odd numbers

Requirements

Given the following array of numbers

var nums =  [3,4,2,1,5,7,9,8]

write the code snippet to sum up all the odd numbers using the half-open range operator:

Demo

var nums =  [3,4,2,1,5,7,9,8]
var sumOfOdds = 0
for i in 0 ..< nums.count {
   if nums[i] % 2 == 1 {
      sumOfOdds += nums [i]//from  w w  w  .ja va  2 s .c  o m
   }
}
print(sumOfOdds)

Result

Related Exercise