Counting valleys traversed below sea level, given elevation changes
There's nothing wrong with or "not functional" about your approach. This might be opinion territory, but personally I would consider using foldLeft as it might be more readable and you can potentially eliminate some of the complexity of your cases.

(also I got rid of n here as it is completely irrelevant, I know it's a problem from a website so you have to follow the format they gave you)

def countingValleys (steps: String): Int = steps
  .foldLeft((0, 0)) {
    case ((valleys, elevation), 'D') =&gt; (valleys, elevation - 1)
    case ((valleys, -1), 'U') =&gt; (valleys + 1, 0)
    case ((valleys, elevation), _) =&gt; (valleys, elevation + 1)
  }
  ._1