I'm new to Swift and don't much experience programming to begin with, and I have written this code with the intention of converting a binary input into a base 10 output. Can anyone tell me where the breakdown is and explain how to do it correctly? Much appreciated.
func toBaseTen(baseTwo: String) -> String{
let reversed = String(baseTwo.characters.reverse()) //cast to String or else returns a ReverseCollection
let toArray = reversed.characters.map { String($0) }
var finalString = [String]()
for element in toArray {
let exponent = Double(toArray.indexOf(element)!)
let elementToBeAdded = { String(pow(Double(element)!, exponent)) }
if element == "1" {
finalString.append(elementToBeAdded())
}
}
finalString.reduce("", combine: +)
return String(finalString)
}