A question I came across the other day during a JavaScript application test was this:
How would you select the n-th character from the word "Exam...
For further actions, you may consider blocking this person and/or reporting abuse
Yes, the +1 isn't needed.
I'm pretty sure you forgot about regexp method :)
Fun fact: in this area Raku language adds another animal to cabinet of curiosities, allowing to specify n-th match as modifier:
Which is actually pretty useful in everyday use.
Now, that would be really slow
Of course. Spinning up regular expression engine to do something that trivial is waste of CPU. Chopping string into characters is also not optimal because it copies unused characters in memory.
My post was simply adding another funny way to pile of TIMTOWTDI.
Javascript provides an awful lot of redundant ways to solve the same task, and I´m not sure if this really is an advantage. Finally, a programming language is just a tool to tell the computer, what to do. Using different ways to do the same thing is simply confusing.
Unless there is no real disadvantage, I would recommend to do things most straight forward.
If you want to create a function, you should be able to use the "function" keyword, as this is what you want the computer to make. But JS provides a bunch of different ways to define functions and sometimes it takes me hours to find out, that the result finally will be a function. Funny, but not efficient...
It is a great example how hard it is to design a language and how you need to be diligent in adding new features. JavaScript had a few convenience methods added to be like abstraction libraries over the years. This is a problem, as you can't deprecate those. Then again, if you look at PHP, it is worse and yet it powers a lot of the web.
The programming lanuage FORTH was know to regularly win programming contests for real time programming. But it could also happen, that a programmer was not able to understand his own code two weeks after he had written it.
You can write code in any language, there is a really funny talk about exotic programming languages presented by Join Dylan Beattie, the creator of "Rockstar", that uses Lyrics to write programs.
It is important to see, that a language can have an impact on our programming style, on our productivity and on the maintainability of the code. You can write amazing compact code in Javascript, but this will be as hard to read as assembler. Is this really useful?
We can improve code quality only by adding comments. The same is true if we just use the most simple approach to write programs. It just enhances readability!
JavaScript has the
charAt
function that accomplishes the same thing:With both JS and PHP you also need to be careful about support for multibyte characters.
Attention ⚠️
Your suggestion of using index existence for checking string length is a neat trick. It not only shortens the code but also adds a readable alternative to using the length property. 👏
Now test this out with an emoji in the string to get the emoji character. ;-)