{"id":200,"date":"2019-12-13T09:49:12","date_gmt":"2019-12-13T09:49:12","guid":{"rendered":"https:\/\/www.samarthya.me\/wps\/?p=200"},"modified":"2019-12-13T09:49:12","modified_gmt":"2019-12-13T09:49:12","slug":"optimizing-in-js","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2019\/12\/13\/optimizing-in-js\/","title":{"rendered":"Optimizing in JS"},"content":{"rendered":"<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-medium wp-image-202\" src=\"https:\/\/www.samarthya.me\/wps\/wp-content\/uploads\/2019\/12\/IMG_20191204_212744-scaled-e1576230534272-281x300.jpg\" alt=\"Sonic the hedgehog\" width=\"281\" height=\"300\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2019\/12\/IMG_20191204_212744-scaled-e1576230534272-281x300.jpg 281w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2019\/12\/IMG_20191204_212744-scaled-e1576230534272-960x1024.jpg 960w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2019\/12\/IMG_20191204_212744-scaled-e1576230534272-768x819.jpg 768w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2019\/12\/IMG_20191204_212744-scaled-e1576230534272-1440x1536.jpg 1440w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2019\/12\/IMG_20191204_212744-scaled-e1576230534272.jpg 1920w\" sizes=\"(max-width: 281px) 100vw, 281px\" \/><\/p>\n<p>You never stop learning is not just a phrase, it is something I live everyday. Today, I was just trying out my skills with JS and was solving an innocuous problem, in JS.<\/p>\n<blockquote>\n<p style=\"text-align: center;\">Pangram: A pangram is a sentence using every letter of the alphabet at least once.<\/p>\n<p style=\"text-align: center;\">E.g. The quick brown fox jumps over the lazy dog.<\/p>\n<\/blockquote>\n<p>Like every old school programmer I was trying to solve it in the easiest of ways without breaking a sweat and the first version I could come up with was as under.<\/p>\n<div>\n<pre>\/\/ Define an string of all Alphabets.\r\nexport const ALPHABETS = \"abcedfghijklmnopqrstuvwxyz\";\r\n\r\n\/\/ isPangram allows you input a string to be validated for Pangram\r\nexport const isPangram = (pangram) =&gt; {\r\n\r\n  if(pangram == null || pangram.length == 0) {\r\n\r\n    return false;\r\n\r\n  }\r\n\r\n  pangram = pangra.toLowerCase();\r\n\r\n  var x;\r\n\r\n  for (x in ALPHABETS){\r\n    if( pangram.indexOf(ALPHABETS[x]) &gt;= 0) {\r\n      continue;\r\n    } else {\r\n      return false;\r\n    }\r\n  }\r\n  return true;\r\n};<\/pre>\n<p>So v1 was not much fat code,\u00a0 I was thinking but then the geek in me was trying to ask can it be simplified further? Optimized in a way it looks cool? and so started my struggle to better the version 1, I had just written.<\/p>\n<\/div>\n<h2>Problem 1: How to generate the character set<\/h2>\n<p>So I thought, instead of having a simple constant string, how about I make it a function to first generate an Array that has all the characters from English; all from A &#8230; Z. (Some helpful <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/from\">link<\/a>)<\/p>\n<div>\n<pre>Array.from({length: 26}, (_, i) =&gt; { String.fromCodeCode(97 + i)});<\/pre>\n<p>How I used it was like this.<\/p>\n<div>\n<pre>const Alphabets = () =&gt; Array.from({length:26}, (_, i) =&gt;String.fromCharCode(97+i));<\/pre>\n<\/div>\n<p>Now, after having this Array it was time to look for the &#8216;Pangram&#8217; in the string. Another method <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/every\"><em>every<\/em>()<\/a> and <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/String\/includes\">includes<\/a>() came to the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/every\">rescue<\/a>.<\/p>\n<h3>V2.0<\/h3>\n<div>\n<pre>\/\/ isPangram allows you to look for Pangram.\r\nexport const isPangram = (pangram) =&gt; {\r\n     var alphabets = Alphabets();\r\n     var lowerCase = pangram.toLowerCase();\r\n     return alphabets.every( character=&gt;lowerCase.includes(character));\r\n};<\/pre>\n<\/div>\n<p>and Voila, I think made a better version from what I had at v1.0.<\/p>\n<\/div>\n<h5 style=\"text-align: center;\">&#8211; The &#8211; End &#8211;<\/h5>\n<div>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>You never stop learning is not just a phrase, it is something I live everyday. Today, I was just trying out my skills with JS and was solving an innocuous problem, in JS. Pangram: A pangram is a sentence using every letter of the alphabet at least once. E.g. The quick brown fox jumps over [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[19,21,20],"class_list":["post-200","post","type-post","status-publish","format-standard","hentry","category-others","tag-js","tag-learn","tag-pangram"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=200"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/200\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}