{"componentChunkName":"component---src-templates-post-template-js","path":"/posts/reversing-string-in-javascript","result":{"data":{"markdownRemark":{"id":"c169e9bc-7b22-5efd-a8e7-c16b8b87f801","html":"<p>Lately, I’ve been doing some coding challenge and I saw this problem. This is fairly easy challenge and the common solution to\nthis is something like this</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">function reverse(s){\n    return s.split(&quot;&quot;).reverse().join(&quot;&quot;);\n}</code></pre></div>\n<p>or with ES6 using reduce function</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">function reverse(s) {\n\n    return s.split(&#39;&#39;).reduce((reversedString, nextCharacter) =&gt; \n        nextCharacter + reversedString\n    , &#39;&#39;);\n}</code></pre></div>\n<p>Easy peasy.</p>\n<p>After answering this problem, I started searching on Stackoverflow because I’m on curious how others solved this problem.</p>\n<p>Then I saw this, <a href=\"https://stackoverflow.com/questions/958908/how-do-you-reverse-a-string-in-place-in-javascript#answer-16776621\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">https://stackoverflow.com/questions/958908/how-do-you-reverse-a-string-in-place-in-javascript#answer-16776621</a> </p>\n<p>It makes my solution and others <em>wrong</em> because of this unicode problem.</p>\n<p>So basically if I used my function with this string</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">reverse(&#39;foo 𝌆 bar mañana mañana&#39;);</code></pre></div>\n<p>The output will be</p>\n<p><img src=\"/images/reverse.png\"></p>\n<p>With a slight change on my ES6 solution, we can fix this problem by using <em>spread operator</em> because this operator is unicode-aware.</p>\n<p>My new solution is now like this</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">function reverse(s) {\n    return [...s].reduce((reversedString, nextCharacter) =&gt; \n        nextCharacter + reversedString\n    , &#39;&#39;);\n}</code></pre></div>\n<p>More info:</p>\n<p><a href=\"https://mathiasbynens.be/notes/javascript-unicode\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">https://mathiasbynens.be/notes/javascript-unicode</a></p>","fields":{"slug":"posts/reversing-string-in-javascript","tagSlugs":["/tag/javascript/","/tag/es-6/","/tag/codingchallenge/","/tag/today-i-learned/"]},"frontmatter":{"date":"2019-03-05T00:00:00.000Z","description":"Lately, I’ve been doing some coding challenge and I saw this problem. This is fairly easy challenge and the common solution to this is something like this...","tags":["Javascript","ES6","codingchallenge","TodayILearned"],"title":"Solving Reverse String in Javascript with ES6"}}},"pageContext":{"slug":"posts/reversing-string-in-javascript"}},"staticQueryHashes":["251939775","401334301","4072520377"]}