• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

204
Views
js switch statement follows a pattern

I have a switch statement with 33 cases, each corresponding to a column in a 2d array. The values in the array is the row that will be transitioned too. Is there a cleaner way to write this? each case follows the pattern for letter in alphabet dfarow = [dfarow][n+1]

function FEN2datumType(fen, dfa) {    

//                 alpabet length 33                            
// exampleDFA = [[1, 0, 1, 2, 1, 0, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 2, 1, 0, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0]], 
//              [[1, 1, 2, 2, 1, 0, 1, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 0, 1, 2, 1, 0, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0]],
//              [[2, 1, 2, 2, 1, 0, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 2, 1, 0, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 2, 2]]

dfarow = 0; // start state
for (char in fen) { 
  switch (char) {
    case "r":      //alpabet 0
      dfarow = dfa[dfarow][0];
      break;
    case "n":
      dfarow = dfa[dfarow][1];
      break;
    case "b":
      dfarow = dfa[dfarow][2];
      break;
    case "q":
      dfarow = dfa[dfarow][3];
      break;
    case "k":
      dfarow = dfa[dfarow][4];
      break;
    case "p":
      dfarow = dfa[dfarow][5];
      break;
.....etc...

this is the first DFA I have written, I open to suggestions. my alphabet is a FEN string (Forsyth–Edwards Notation chess) 33 possible chars. exampleDFA has 3 transition states and I have not implemented an accepting state (I was thinking of putting an identifier in the 34th column of the DFA array i.e. if (dfa[dfarow][34] == 1) //accept)

almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can create object for the alphabet:

var alphabet = { r:0, n:1, b:2, .... }

and then you can call alphabet object in for loop:

for (char in fen) { 
  dfarow = dfa[dfarow][alphabet[char]];
}

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error