Closed ageron closed 1 month ago
Maybe this is the approach you already tried, but it looks like it wouldn't be too bad to extract a common function that accepts a comparison function, increment function, and bestValue that could then be used by largest and smallest.
Either way, looks good!
Yes, that's what I tried, but it ended up making the code longer and less readable, unfortunately, probably because the functions differ in several little places. I chose to leave it as is, but if you manage to do better, I'd love to see it and we could update the solution. 👍
This one was surprisingly tricky: getting the tests to pass was not that hard, but my solution was way too slow, it took almost a minute to run. Since Exercism rejects solutions that take more than ~20 seconds to run, I had to optimize my code. I managed to get it to run in less than a second, but unfortunately the code is now much less pleasant to read. In particular, the
smallest
andlargest
functions are very similar. I tried to factorize some things, but the code ended up being harder to read and understand, so it didn't really help, and I reverted to this version. I'm sure there must be a much better way to solve this exercise, but I didn't find it. Because of this, I set the difficulty to 6, even though getting the tests to pass initially isn't that hard. The difficulty is finding a clean and fast solution.