PARSING
ALGORITHMS
Write a simple parser that will parse and run Deadfish.
Deadfish has 4 commands, each 1 character long:
i
increments the value (initially0
)d
decrements the values
squares the valueo
outputs the value into the return array
Invalid characters should be ignored.
parse("iiisdoso") => [ 8, 64 ]
const parse = data => {
let currentValue = 0
const result = []
data.split('').forEach(command => {
if (command === 'i') currentValue++
if (command === 'd') currentValue--
if (command === 's') currentValue = Math.pow(currentValue, 2)
if (command === 'o') result.push(currentValue)
})
return result
}