Multiply Array position Swift

I need to fix this code in Swift language. I don’t know how can I to multiply array positions mut1 and mut2 values. The result is 8.

var mut1 = [7,-4,5]
var mut2 = [3,2,-1]

var multiply = mut1[0] * mut2[0] + mut1[1] * mut2[1] + mut1[2] * mut2[2]

print(multiply)

>Solution :

If you want a Swift-like solution, you could try this:

var mut1 = [7,-4,5]
var mut2 = [3,2,-1]

var multiply = zip(mut1, mut2).map { $0 * $1 }.reduce(0, +)

print(multiply) // 8

Leave a Reply