You are currently viewing HI guys! I’m working on the direction reduction exercise and I thought I’ve finally figured it out but am having an issue with the “results.dropLast(1)” . For some reason it’s not dropping “SOUTH” as it should. (ive printed “Results” before and after to see). Can someone please explain the issue?

HI guys! I’m working on the direction reduction exercise and I thought I’ve finally figured it out but am having an issue with the “results.dropLast(1)” . For some reason it’s not dropping “SOUTH” as it should. (ive printed “Results” before and after to see). Can someone please explain the issue?

my code:

fun main() { val arr = arrayOf("NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST") var results = arrayOf<String>("placeholder") var counter = 0 fun checkSkip(): Boolean { if (counter == 0) { return false } else { return true } } for (i in 0..arr.size -2) { if (arr[i] == "NORTH" && arr[i + 1] == "SOUTH" || arr[i] == "SOUTH" && arr[i + 1] == "NORTH" || arr[i] == "WEST" && arr[i + 1] == "EAST" || arr[i] == "EAST" && arr[i + 1] == "WEST") { counter += 1 } else if (arr[i] == "NORTH" && arr[i - 1] == "SOUTH" || arr[i] == "SOUTH" && arr[i - 1] == "NORTH" || arr[i] == "WEST" && arr[i - 1] == "EAST" || arr[i] == "EAST" && arr[i - 1] == "WEST" && checkSkip() == true) { counter -= 1 } else { if (arr[i] == "NORTH" && results[results.size - 1] == "SOUTH" || arr[i] == "SOUTH" && results[results.size - 1] == "NORTH" || arr[i] == "WEST" && results[results.size - 1] == "EAST" || arr[i] == "EAST" && results[results.size - 1] == "WEST") { println(results.joinToString()) results.dropLast(1) println(results.joinToString()) counter -= 1 } else { results += arr[i] counter += 1 } } } println(results.drop(1).joinToString()) if (checkSkip() == false) results += arr[arr.size - 1] println(results.drop(1).joinToString()) } 

submitted by /u/SkeeterIsBlue
[link] [comments]