Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                /**
 * Thanks for this FSM implementation to Zakhar Ovcharov (https://www.linkedin.com/in/zakhar-ovcharov-a657a4197/)
 * I did small adjustments to meet requirements of the specific task
 */

class FSM {
  /**
   * Each instance of this class will have a certain alphabet, a list of states and transitions, and could be used to test some value against these params
   * @param {String} alphabet
   * @param {String[]} states
   * @param {String} startState
   * @param {String[]} finiteStates
   * @param {Object} transitions
   */
  constructor(alphabet, states, startState, finiteStates, transitions) {
    this.alphabet = alphabet
    this.states = states
    this.startState = startState
    this.transitions = transitions
    this.finiteStates = finiteStates
    this.currentState = null
  }

  /**
   * Check if the symbol belongs to the specified alphabet
   * @param {String} symbol
   * @returns {Boolean}
   */
  _checkIsBelongAlphabet(symbol) {
    return this.alphabet.includes(symbol)
  }

  /**
   * Change the current state depending on the symbol. FSM should abort if there is no transition for the current symbol on the current state
   * @param {String} symbol
   */
  _changeState(symbol) {
    if (
      this.transitions[this.currentState] &&
      this.transitions[this.currentState][symbol]
    ) {
      this.currentState = this.transitions[this.currentState][symbol]
    } else {
      throw new Error(`No transition from ${this.currentState} by ${symbol}`)
    }
  }

  /**
   * Test some value against specified params. Accepts value if the finiteStates array includes the last current state and rejects otherwise
   * @param {String} value
   * @returns {String}
   */
  test(value) {
    this.currentState = this.startState

    for (let symbol of value) {
      if (this._checkIsBelongAlphabet(symbol)) {
        this._changeState(symbol)
      } else {
        return false
      }
    }

    return this.finiteStates.includes(this.currentState)
  }
}

// Params in order: alphabet, states, initial state, finite states, transitions
// We have to define transitions as an object, where:
// 1. Key is the state
// 2. Value is the object, where:
// 2.1. Key is the symbol
// 2.2. Value is the target state
const fsm = new FSM('01', ['q0', 'q1'], 'q0', ['q0'], {
  q0: {
    0: 'q0',
    1: 'q0'
  },
  q1: {
    0: 'q1',
    1: 'q1'
  }
})

console.log(fsm.test('111')) // true
console.log(fsm.test('0111')) // true
console.log(fsm.test('1112')) // false, 2 doesn't belong to the alphabet '01'

              
            
!
999px

Console