How do you write the Viterbi algorithm in JavaScript?
Here's an implementation of the Viterbi algorithm in JavaScript:
```javascript
function viterbi(obs, states, start_p, trans_p, emit_p) {
  let V = [];
  for (let i = 0; i < states.length; i++) {
    V[i] = [];
  }

  for (let i = 0; i < states.length; i++) {
    V[i][0] = start_p[i] * emit_p[i][obs[0]];
  }

  for (let t = 1; t < obs.length; t++) {
    for (let j = 0; j < states.length; j++) {
      let max_tr_prob = V[0][t-1] * trans_p[0][j];
      let prev_state_selected = 0;
      for (let i = 1; i < states.length; i++) {
        let tr_prob = V[i][t-1] * trans_p[i][j];
        if (tr_prob > max_tr_prob) {
          max_tr_prob = tr_prob;
          prev_state_selected = i;
        }
      }
      V[j][t] = max_tr_prob * emit_p[j][obs[t]];
    }
  }

  let opt = [];
  let max_prob = V[0][obs.length - 1];
  let state_selected = 0;

  for (let i = 1; i < states.length; i++) {
    if (V[i][obs.length - 1] > max_prob) {
      max_prob = V[i][obs.length - 1];
      state_selected = i;
    }
  }

  opt.push(state_selected);
  for (let t = obs.length - 2; t >= 0; t--) {
    state_selected = 0;
    for (let i = 0; i < states.length; i++) {
      if (V[i][t] * trans_p[i][state_selected] === V[state_selected][t + 1]) {
        state_selected = i;
      }
    }
    opt.unshift(state_selected);
  }

  return {
    prob: max_prob,
    path: opt
  };
}
```
This implementation takes as input the observation sequence obs, a list of states, the start_p probability distribution, the trans_p transition probability matrix, and the emit_p emission probability matrix. It returns an object with the maximum probability prob and the state path path.