Skip to the code
cf.
+10 XP

#153 Last Error in the Console

easy

A browser console panel keeps the level of every message it has printed, oldest first, in levels. Each level is one of the strings "info", "warn", or "error", but some tools also log odd levels such as "Error".

Write lastError(levels) that returns the index of the most recent message whose level is exactly "error" (lowercase), or -1 if there is none.

Example 1

Input: levels = ["info","error","warn","error","info"]
Output: 3

Errors are at 1 and 3. The most recent one is at index 3.

Example 2

Input: levels = ["info","warn"]
Output: -1

No message is an error.

Constraints

  • 0 <= levels.length <= 10000
  • Each level is a non-empty string.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).