Skip to content

Latest commit

 

History

History
43 lines (31 loc) · 946 Bytes

make-the-deadfish-swim.md

File metadata and controls

43 lines (31 loc) · 946 Bytes

Make the Deadfish Swim 6 Kyu

LINK TO THE KATA - PARSING ALGORITHMS

Description

Write a simple parser that will parse and run Deadfish.

Deadfish has 4 commands, each 1 character long:

  • i increments the value (initially 0)
  • d decrements the value
  • s squares the value
  • o outputs the value into the return array

Invalid characters should be ignored.

parse("iiisdoso") => [ 8, 64 ]

Solution

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
}