javascript algorithm interview questions code example

Example 1: javascript advanced interview questions

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addNode</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-keyword">var</span> newP = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">"p"</span>);
										<span class="hljs-keyword">var</span> textNode = <span class="hljs-built_in">document</span>.createTextNode(<span class="hljs-string">" This is a new text node"</span>);
										newP.appendChild(textNode); <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"firstP"</span>).appendChild(newP); }

Example 2: javascript interview questions in github

var object = new Object();

Example 3: javascript algorithms and data structures interview questions

function isPrime(n)
{
  var divisor = 3, 
      limit = Math.sqrt(n);
  
  //check simple cases
  if (n == 2 || n == 3)
     return true;
  if (n % 2 == 0)
     return false;

  while (divisor <= limit)
  {
    if (n % divisor == 0)
      return false;
    else
      divisor += 2;
  }
  return true;
}

> isPrime(137);
  = true
> isPrime(237);
  = false