/* THIS CODE HAS A BUG, CAN YOU FIND IT ? */ /* HINT: It's about scope of variables. Do you know how variable declaration in Groovy works? What is difference between variable declaration with and without "def" keyword ? */ decimalValue = 9989 hexValue = toHex(code) assert hexValue == '2705' // safe net expectedValue = Integer.toHexString(decimalValue) assert hexValue == expectedValue : "Your impl should work as expected. Result should be: ${expectedValue}" println "DECIMAL VALUE: ${decimalValue} CONVERTED TO HEX VALUE: ${hexValue}" /* ------------------------ METHODS -------------------------- */ /* note you can use Java's Integer#toHexString() this is just for practicing purposes */ def toHex(number) { next = Double.valueOf(Math.floor(number / 16)).toLong() remainder = number % 16 // debug print to see what happens inside of this method // println next + ", " + remainder // translate 10 -> A, 11->B .... 15->F if (remainder > 9) { remainder = (((int)'A') + (remainder - 10)) as char } // end recursion if (next == 0) { return remainder } return toHex(next) + remainder.toString() }