{"id":889,"date":"2012-01-04T00:59:30","date_gmt":"2012-01-04T06:59:30","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=889"},"modified":"2012-01-04T01:21:06","modified_gmt":"2012-01-04T07:21:06","slug":"code-of-the-day-sentence-scrambler","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2012\/01\/code-of-the-day-sentence-scrambler\/","title":{"rendered":"Code of the Day: Sentence Scrambler"},"content":{"rendered":"<p><b>Task:<\/b><br \/>\nThere was a study going out a few years ago that said people can read words that are scrambled if the first and last character are left in place. So to help me practice Test Driven Development, I decided program it.<br \/>\nExample:<br \/>\nCorrect: &#8220;Create attractive offers to reach the right customers&#8221;<br \/>\nScrambled: &#8220;Cterae aacrtvitte offres to recah the rghit couemtrss&#8221;<\/p>\n<p>For more information about the study visit: <a href=\"Cambridge Word Scramble Study: It's Fake Already!\">Cambridge Word Scramble Study: It&#8217;s Fake Already!<\/a><\/p>\n<p><b>Code:<\/b><\/p>\n<pre lang='javascript'>\r\n\/**\r\n* @author: Larry Battle\r\n* @date: January 2, 2012\r\n* @purpose: Scrambler - to scramble the alphabetical letters in word in such a way to make it still readable and understandable.\r\n* @note: This can be accomplished by have perversing the word order but mixing up every letter in the word except for the first.\r\n*\/\r\nvar scrambler = {\r\n\tscrambleStr : function( str ){\r\n\t\tif( typeof str !== 'string' ){\r\n\t\t\treturn -1;\r\n\t\t}\r\n\t\treturn str.replace( \/[^\\s]*\/g, function( word ){\t\t\t\r\n\t\t\treturn word ? scrambler.scrambleWord( word ) : word;\r\n\t\t});\r\n\t},\r\n\tscrambleWord :  function( str ){\r\n\t\tif( typeof str !== 'string' ){\r\n\t\t\treturn str;\r\n\t\t}\r\n\t\tstr = str.replace( \/[a-z]*\/ig, function( str2 ){\r\n\t\t\tif( str2 && str2.length > 2 ){\r\n\t\t\t\tstr2 = str2.charAt(0) + scrambler.randomizeStr( str2.substring( 1, str2.length - 1 ) ) + str2.charAt( str2.length - 1 );\r\n\t\t\t}\r\n\t\t\treturn str2;\r\n\t\t});\r\n\t\treturn str;\r\n\t},\r\n\trandomizeStr : function( str ){\r\n\t\tif( typeof str !== 'string' ){\r\n\t\t\treturn str;\r\n\t\t}\r\n\t\treturn scrambler.makeArrayRandom( str.split( '' ) ).join( '' );\r\n\t},\r\n\tmakeArrayRandom : function( arr ){\r\n\t\tvar j, x, i = arr.length;\r\n\t\twhile( i ){\r\n\t\t\t\tj = parseInt(Math.random() * i, 10);\r\n\t\t\t\tx = arr[--i]; \r\n\t\t\t\tarr[i] = arr[j];\r\n\t\t\t\tarr[j] = x;\r\n\t\t}\r\n\t\treturn arr;\r\n\t}\r\n};\r\n<\/pre>\n<p><b>Input:<\/b><\/p>\n<pre lang=\"\">\r\nvar str = \"I can't believe what I'm reading.\"\r\nscrambler.scrambleStr( str );\r\n<\/pre>\n<p><b>Output:<\/b> (Possible outcome)<\/p>\n<pre lang=\"dos\">\r\n\"I can't beilvee waht I'm rndiaeg.\"\r\n<\/pre>\n<p>Try it out for yourself.<br \/>\nDemo: <a href=\"http:\/\/www.bateru.com\/projects\/scrambler\/\">Scrambler<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Task: There was a study going out a few years ago that said people can read words that are scrambled if the first and last character are left in place. So to help me practice Test Driven Development, I decided program it. Example: Correct: &#8220;Create attractive offers to reach the right customers&#8221; Scrambled: &#8220;Cterae aacrtvitte &hellip; <a href=\"https:\/\/bateru.com\/news\/2012\/01\/code-of-the-day-sentence-scrambler\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the Day: Sentence Scrambler<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[30,164],"class_list":["post-889","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","tag-code-of-the-day","tag-javascript"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=889"}],"version-history":[{"count":5,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/889\/revisions"}],"predecessor-version":[{"id":891,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/889\/revisions\/891"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}