In the first method you aren't doing an assignment, are sending the message = to the temp isValid, if you change the method to: decimalFromBinary: aString | result isValid | * isValid := self isValidBinary: aString.* isValid ifTrue: [ result := 0. aString reverse withIndexDo: [ :digit :index | result := result + (digit digitValue * (2 raisedTo: index - 1)) ]. ^ result ] ifFalse: [ ^ nil ] it should work. In the second one you're comparing characters with numbers, this will always return false because the number 0 is not the same as the character 0. Use isValidBinary: aString ^ aString allSatisfy: [ :c | c = $0 or: [ c = $1 ] ] or something like isValidBinary: aString ^ aString allSatisfy: [ :c | '01' includes: c ] On Sun, Apr 26, 2020 at 2:52 PM Roelof Wobben via Pharo-users < pharo-users@lists.pharo.org> wrote:
Hello,
I have to make some code that convert a binary to a decimal and im not allowed to use the convert methods that Pharo has.
So I have written this :
decimalFromBinary: aString | result isValid | isValid = self isValidBinary: aString. isValid ifTrue: [ result := 0. aString reverse withIndexDo: [ :digit :index | result := result + (digit digitValue * (2 raisedTo: index - 1)) ]. ^ result ] ifFalse: [ ^ nil ]
isValidBinary: aString ^ aString allSatisfy: [ :c | c = 0 or: [ c = 1 ] ]
but on the first method I see a message that the temp variables are read before written. and the second one I see a message that I use a or instead of searching literals.
Where did I think wrong here ?
Roelof