karan satia karan satia - 7 months ago 12
Javascript Question

JS convert delimiter separated string to array elements

Just for fun, I wrote a quick script to take in a string with comma separated characters, I use the commas to denote a different word, and I output an array with the words as elements. My issue is with my logic - this only works for commas in the original string.

What is happening is that the algorithm only looks at the first element (a comma) in the delimiter array that I provide when calling my function. I'm thinking about using a boolean as a flag somewhere, but not sure if this is really the best way to go about it. Any advice?



var stringToArray = function(delimiterArray, originalString) {
var arrayOutput = []
var tempWord = ""
for (var i = 0; i < originalString.length; i++) {

for (var j = 0; j < delimiterArray.length; j++) {

var currentCharacter = originalString.charAt(i)
var currentDelimiter = delimiterArray[j]

while (currentCharacter != currentDelimiter) {
tempWord += currentCharacter
//once we hit a delimiter, break so that we can move onto the next conditional statement.
break
}

if (currentCharacter === currentDelimiter) {
//push word onto the array to hold each string, and then break out so we can go back to the iteration of the nested loops
arrayOutput.push(tempWord)
tempWord = ""
break
}

//we break out of the second for loop ->
break
}

}
return arrayOutput
}

delims = [',', '.', ';', ' ']
originalString = "USA,Canada,Mexico,Bermuda,Grenada,Belize"

finalOutput = stringToArray(delims, originalString)
console.log(finalOutput)





If I use a '.' as a delimiter in the string that I'm using as an argument, my algorithm fails. I implemented this in C using boolean flags (a long time ago) where I had to write way more code because I wasn't using any built in functions (had to find the length of everything so I could malloc enough memory for data structures to store stuff). Don't want to revisit that old code, though.

Answer

The problem is that you're adding the current character to tempWord if it doesn't match the current delimiter. So if the current character doesn't match the first delimiter, it gets added to tempWord, even though it might match one of the other delimiters. You need to go through the entire delimiter array before you can determine if the character is a delimiter or not.

var stringToArray = function(delimiterArray, originalString) {
  var arrayOutput = [];
  var tempWord = "";
  for (var i = 0; i < originalString.length; i++) {
    var currentCharacter = originalString.charAt(i)
    var isDelimiter = false;
    for (var j = 0; j < delimiterArray.length; j++) {

      var currentDelimiter = delimiterArray[j]

      if (currentCharacter == currentDelimiter) {
        isDelimiter = true;
        //once we hit a delimiter, break so that we can move onto the next conditional statement. 
        break;
      }

    }
    if (isDelimiter) {
      arrayOutput.push(tempWord);
      tempWord = "";
    } else {
      tempWord += currentCharacter;
    }

  }
  return arrayOutput;
}

delims = [',', '.', ';', ' ']
originalString = "USA,Canada.Mexico,Bermuda,Grenada,Belize"

finalOutput = stringToArray(delims, originalString)
console.log(finalOutput)

For some reason, this seems to be an incredibly common error by people writing their own array searching code.