DEV Community

loading...

Discussion on: A JavaScript interview question asked at Facebook

Collapse
richardeschloss profile image
Richard Schloss

Also, I think the removeBackspaces method can further be reduced with reduce:

function removeBackspaces(arr) {
  return arr.reduce((result, val) => {
    val === '-B' ? result.pop() : result.push(val)
    return result
  }, [])
}

Gives me the same result, I think, and helps guard against typical problems introduced by loops, such as "off-by-one" errors. (array.reduce helps the iteration stay within the bounds of the array size)