Are You Solving the Right Problem? (i.e. the XY Problem)
Ever heard of the XY problem? No? Those of you in tech support may have seen it without knowing its name, but it actually happens quite often in programming and tech support. Here is an example from xyproblem.info , albeit rewritten for JavaScript n00b> How do I get the last 3 letters of the filename? feline> If it's in a variable, you can slice it like str.slice(-3) feline> Wait, why are you asking? What do you really want? feline>Do you want the extension (of a file)? n00b> Uh, yes? feline>There's no guarantee that every file name will have a three-letter extension, so blindly grabbing three characters does not solve the problem. With that said, here are three ways to get it , using regex, split, or slice+lastIndexOf See the problem? noob asks for X (last 3 characters of string), but actually wanted to solve Y (get file extension). What if there was no extension? What if the extension is MORE THAN 3 letters? S...