2
\$\begingroup\$

Hello I am hoping someone can provide me feedback on my solution to this prob. If there are additional edge cases I should consider etc. Thank you!

The question: "Given a sentence with a set of delimiters, reverse the order of the words in the sentence and keep the delimiters in place."

let isAlpha = (char) => {
  let pat = /[a-zA-z]/igm;     // match any alphabetic character
  if(char.match(pat)){
      return true
  } else {
      return false
  }
}

function reverse(s){
  
  let specialArr = []   

  for(ltr of s){ 
    if(!isAlpha(ltr)){                        // pull out all special characters in order
      specialArr.push(ltr)
    } 
  }
  
  let regCharArr = s.split(/[^a-z]/ig).reverse()     // use split to pull out all words separated by non alpha characters. then reverse
  
  let result = []                                    // init result array
  
  if(isAlpha(s[0])){
    result = regCharArr.flatMap((val, idx) => [val, specialArr[idx]])   //use flatmap to combine the two arrays...if origin array starts with letter, combine special array into reg array
  }
  
  if(!isAlpha(s[0])){
    result = specialArr.flatMap((val, idx) => [val, regCharArr[idx]])    // vice versa
  }

  for(let i = 0; i < result.length; i++){         // remove any undefineds
    if(result[i] === undefined){
      result.splice(i, 1)
    }
  }
  return result.join('')
}


// let sentence = "one|twotwo/three:four"  // four|three/twotwo:one
let sentence = "|brian/steve:chris|jon"     // |jon/chris:steve|brian

console.log(reverse(sentence))

```
\$\endgroup\$
3
  • \$\begingroup\$ There is an answer at SO \$\endgroup\$ Commented Feb 9, 2021 at 19:43
  • \$\begingroup\$ Thank you! I just took a look at your response. My solution breaks on spaces too..where as yours does not. \$\endgroup\$
    – zguerrin27
    Commented Feb 9, 2021 at 20:24
  • 1
    \$\begingroup\$ Could you add some testcases to describe what the requirement is? For example, what is "word" means here? Is "twenty-three" a single word or two? Will there always be some words between two delimiter characters? What is the expected output for "#a!!b?c"? \$\endgroup\$
    – tsh
    Commented Feb 10, 2021 at 6:11

1 Answer 1

2
\$\begingroup\$

The current implementation of isAlpha ...

let isAlpha = (char) => {
  // match any alphabetic character.
  let pat = /[a-zA-z]/igm;

  if(char.match(pat)){
    return true;
  } else {
    return false;
  }
};

... can be refactored into something as short as this ...

function isAlpha(char) {
  return (/[a-zA-z]/).test(char);
};

... or maybe even that ...

function isAlpha(char) {
  // \w matches any word character
  // and is equal to [a-zA-Z0-9_]
  return (/\w/).test(char);
};

Why is that?

isAlpha gets invoked from within reverse always by providing a single character. Thus the regex can be simplified from /[a-zA-z]/igm to /[a-zA-z]/. Since isAlpha is supposed to return a boolean value do not use match and do no not return either true or false separately but use RegExp.prototype.test instead which already provides the correct return value.

Thats all so far but I will continue looking through the code, since as of now I don't understand why one wants to test a string stepwise character by character.

There is an answer of mine, regarding your question at StackOverflow. Maybe you get some inspiration from an alternative approach.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.