{"id":7943,"date":"2011-02-09T12:07:09","date_gmt":"2011-02-09T17:07:09","guid":{"rendered":"https:\/\/htmlcssjs.wpengine.com\/?p=7943"},"modified":"2014-10-08T16:37:25","modified_gmt":"2014-10-08T20:37:25","slug":"another-front-end-engineer-interviewing-question-loop-alternatives","status":"publish","type":"post","link":"https:\/\/htmlcssjavascript.com\/javascript\/another-front-end-engineer-interviewing-question-loop-alternatives\/","title":{"rendered":"Another Front End Engineer Interviewing Question: Loop Alternatives"},"content":{"rendered":"<p>We&#8217;ve actually hired a couple of people recently, so I&#8217;m not sure if I&#8217;ll be able to bust this one out any time soon. Instead, I&#8217;ll share it with you. <\/p>\n<p>This is another one that I feel works for a lot of different experience and knowledge levels. A good, junior developer should be able to give me a satisfactory answer and a senior developer could take the same problem, decide to be clever, and give me a great answer.<\/p>\n<p>Let&#8217;s take a look at it.<\/p>\n<p><!--more--><\/p>\n<h2>The Markup, a Task and a Twist<\/h2>\n<p>I start with the following markup:<\/p>\n<p>[javascript]<br \/>\n&amp;lt;div&amp;gt;div 1&amp;lt;\/div&amp;gt;<br \/>\n&amp;lt;div&amp;gt;div 2&amp;lt;\/div&amp;gt;<br \/>\n&amp;lt;div&amp;gt;div 3&amp;lt;\/div&amp;gt;<br \/>\n&amp;lt;div&amp;gt;div 4&amp;lt;\/div&amp;gt;<br \/>\n&amp;lt;div&amp;gt;div 5&amp;lt;\/div&amp;gt;<br \/>\n[\/javascript]<\/p>\n<p>The task is simple. Using plain JavaScript, add a 1px border to each of those divs.  The only catch is, the <code>for<\/code> loop is forbidden. <\/p>\n<p>Depending on the level of the candidate, there are several answers that will bring a smile to my face.<\/p>\n<h2>While Loop<\/h2>\n<p>This is a while loop that basically takes the place of a <code>for<\/code> loop.<\/p>\n<p>[javascript]<br \/>\nvar divs = document.getElementsByTagName(&amp;quot;div&amp;quot;),<br \/>\n     i = 0;<br \/>\nwhile (i &amp;lt; divs.length) {<br \/>\n\u00a0 \u00a0\u00a0divs[i].style.border= &amp;quot;1px solid blue&amp;quot;<br \/>\n\u00a0 \u00a0\u00a0i++;<br \/>\n}<br \/>\n[\/javascript]<\/p>\n<p>This is a standard &#8220;right&#8221; answer for a junior developer. A junior dev who thinks of using a while loop and can actually craft one is not a phony. This is especially true now since so many people just use $(selector).each without even thinking. <\/p>\n<p>This can be improved. <\/p>\n<h2>Faster While Loop<\/h2>\n<p>This isn&#8217;t much different, but it caches the <code>divs.length<\/code> to save the lookup each time. <\/p>\n<p>[javascript]<br \/>\nvar divs = document.getElementsByTagName(&amp;quot;div&amp;quot;),<br \/>\n      i = 0,<br \/>\n\u00a0\u00a0test=divs.length;<br \/>\nwhile (i &amp;lt; test) {<br \/>\n\u00a0 \u00a0\u00a0divs[i].style.border=&amp;quot;1px solid blue&amp;quot;<br \/>\n\u00a0 \u00a0\u00a0i++;<br \/>\n}<br \/>\n[\/javascript]<\/p>\n<p>This would be a <strong>great<\/strong> answer for a junior developer and a satisfactory answer for someone more senior. <\/p>\n<p>With a senior developer I <em>would<\/em> ask for another alternative. <\/p>\n<h2>Get Wacky With a Do&#8230; While<\/h2>\n<p>Here&#8217;s one potential alternative. I would be surprised to get this as a primary answer, but as a secondary answer it&#8217;d be great.<\/p>\n<p>[javascript]<br \/>\nvar divs = document.getElementsByTagName(&amp;quot;div&amp;quot;),<br \/>\n      i = 0,<br \/>\n  test=divs.length;<br \/>\ndo {<br \/>\n\u00a0 \u00a0\u00a0divs[i].style.border= &amp;quot;1px solid purple&amp;quot;<br \/>\n\u00a0 \u00a0\u00a0i++;<br \/>\n} while (i &amp;lt; test)<br \/>\n[\/javascript]<\/p>\n<h2>Silver Medal- a Fast While Loop<\/h2>\n<p>This one indicates attention to detail and an eye towards performance. <\/p>\n<p>[javascript]<br \/>\nvar divs = document.getElementsByTagName(&amp;quot;div&amp;quot;),<br \/>\n      i = divs.length;<br \/>\nwhile (i&#8211;) {<br \/>\n\u00a0 \u00a0divs[i].style.border= &amp;quot;1px solid   grey&amp;quot;<br \/>\n}<br \/>\n[\/javascript]<\/p>\n<p>This reverse while loop made rounds a few years ago as the outcome of the incredible survey: <a href=\" http:\/\/blogs.sun.com\/greimer\/entry\/best_way_to_code_a\">What&#8217;s the Fastest Way to Code a Loop in JavaScript?<\/a> Someone who either read that or had the results filter down to them is okay in my book. <\/p>\n<h2>The Golden Ticket<\/h2>\n<p>If a more senior developer busts out the below solution, they get the Gold Star.<br \/>\n[javascript]<br \/>\n[ ].forEach.call(divs, function(obj){obj.style.border=&amp;quot;1px solid red&amp;quot;})<br \/>\n[\/javascript]<\/p>\n<p>This shows someone who&#8217;s poked at the guts of JavaScript in a meaningful way. They also get bonus points for pointing out that <code>forEach<\/code> is slower than a <code>for<\/code> loop. <\/p>\n<h2>Bonus: Ignore Me For Fun and Profit<\/h2>\n<p>In addition to any of the above solutions, if a candidate of any level says &#8220;but really, just use CSS,&#8221; even though the instructions say &#8220;use JavaScript,&#8221; they get bonus points because, really, why do it like this?<\/p>\n<h2>JavaScript Ninja\/Pirate\/Jedi Section<\/h2>\n<p>Feel free to follow <a href=\"http:\/\/www.amodernfable.com\/\">Adam<\/a>&#8216;s lead and propose other, interesting solutions in the comments. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve actually hired a couple of people recently, so I&#8217;m not sure if I&#8217;ll be able to bust this one out any time soon. Instead, I&#8217;ll share it with you. This is another one that I feel works for a lot of different experience and knowledge levels. A good, junior developer should be able to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[212,24,23],"class_list":["post-7943","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-interviewing","tag-methodology","tag-philosophy"],"_links":{"self":[{"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/posts\/7943","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/comments?post=7943"}],"version-history":[{"count":0,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/posts\/7943\/revisions"}],"wp:attachment":[{"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/media?parent=7943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/categories?post=7943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/tags?post=7943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}