DEV Community

Discussion on: A JavaScript interview question asked at Facebook

richardeschloss profile image
Richard Schloss • Edited on

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)