Home > katas > Multiply Swift (8kyu)

Multiply Swift (8kyu)


Let's start with Multiply kata. I have to solve this one when I signed up for the Codewars account. It is some kind of validation, I guess.

Chek kata on Codewars

Description:

The code does not execute properly. Try to figure out why.

We start with this code:

func multiply(_ a: Double, _ b: Double) -> Double {
    a * b
}

We can solve it just by adding the return

func multiply(_ a: Double, _ b: Double) -> Double {
    return a * b
}

Or we can do oneliner.

let multiply: (Double, Double) -> Double = { $0 * $1 }

Let's check others solutions.

Some people use reducer to multiply two numbers. As for me, it's a little bit overkill. But some people like it, so why not.

func multiply(_ x: Double...) -> Double {
    return x.reduce(1, *)
}
© 2021, Andrew Losseff