Code Example
To show how this works in essence, we're going to implement the following state machine:

This would accept strings like these:
abc
aaaaaabc
aaabaababc
- etc.
We're going to use a hash map to record possible transitions. A pair of states and input triggering a particular transition is going to be the key, and the next state is going to be value. You can see how our key corresponds to arrows on our diagram, while the value corresponds to circles - states.
Please look over the code now.
Note that in practice we'll probably have more levels of abstraction than this. States might be entire classes instead of simple characters or strings and transitions may produce way more interesting effects than just printing output. But the essence is here - you could expand this code into any state machine you'd like.
xxxxxxxxxx
});
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log('Enter character by character.');
console.log('Allowed characters are: a, b, c.');
console.log('Use Ctrl+D to end input.');
const s1 = '1';
const s2 = '2';
const s3 = '3';
const error = 'error';
let state = s1;
let finish = s3;
let userInput = '';
const transitions = {
[`${s1}a`]: s1,
[`${s1}b`]: s2,
[`${s2}c`]: s3,
[`${s2}a`]: s1
};
rl.on('line', (c) => {
userInput += c;