Switch Statement vs Console.log() discrepency.

Hi, I have a switch statement that takes in a function that when it returns true, still goes via the false case route. I'm not aware as to why or how I can fix it? I've created a codesandbox with a built in terminal and details described in the DOM here: https://codesandbox.io/s/epic-lederberg-puf1v5?file=/index.html
Gold240sx
CodeSandbox
epic-lederberg-puf1v5 - CodeSandbox
epic-lederberg-puf1v5 by Gold240sx using parcel-bundler
7 Replies
Jochem
Jochem3y ago
oof, this was a tricky one... you're testing it multiple times, and when you have the global flag on in a regex, each subsequent call to test will check for an additional match so either you have to make sure you only ever use it once in the switch call, cache the result and use that instead, or remove the g from the regex
Gold240sx
Gold240sxOP3y ago
so the "EML_ORGONLY_REGEX2.test(input)" is the call and after I run it once I need to set the index to zero or something ? is it an error with where it starts?
Jochem
Jochem3y ago
RegExp.prototype.test() - JavaScript | MDN
The test() method executes a search for a match between a regular expression and a specified string. Returns true or false.
Jochem
Jochem3y ago
do you really need the g flag in your case? it doesn't seem like you're doing anything with additional results in this example at least
Gold240sx
Gold240sxOP3y ago
no, I dont but my regex last timie i checked didn't work without it
Jochem
Jochem3y ago
seems to work fine to me the only thing g does is keep going after the first full pattern match if you want to keep the g, just cache the result. Do a const result = EML_ORGONLY_REGEX2.test(input); on line 8 in index.js, and then any other calls you replace with result instead it's generally a good practice to cache results like this anyway. Also, while I'm giving general tips, you shouldn't use var anymore
Gold240sx
Gold240sxOP3y ago
Thank you
Want results from more Discord servers?
Add your server