JSFiddle - React, Tailwind, and code Playground

Alexander Zou - Technical Assessment - Mid-level JavaScript test

by David Lomneck

HTML

<!--don't edit this file-->
<div id=question>
  <span style="font-weight:bold">Question 1:</span> Generate all prime numbers between two given numbers. The two numbers are parameters that can be passed into the function. A prime number is one that is only divisible by 1 and itself. Output the results into the results element below.
  <p>
    Example:
    <br/> Primes between 1 and 10
    <br/> Output:
    <br/> 2
    <br/> 3
    <br/> 5
    <br/> 7
    <br/>
  </p>
</div>
<br/>
<p>Your Results:</p>
<div id="results1"></div>
<br/>
<div id=question>
<span style="font-weight:bold">Question 2:</span> A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest
  palindrome larger than K to output. Numbers are always displayed without leading zeros.
  <P>
    Example:
    <br/> Number Given: 808
    <br/> Output: 818
    <br/> Number Given: 2133
    <br/> Output: 2222
    <br/>
  </P>
</div>
<br/>
<p>Your Results:</p>
<div id="results2"></div>
<br/>
<div id="question">
  <span style="font-weight:bold">Question 3:</span> Write a simple string compression algorithm. If a character [ch] occurs n>1 times in a row, then it will be represented by [ch][n].
  <P>
    Example:
    <br/> String Given: aaaa
    <br/> Output: a4
    <br/>
    <br/> String Given: aaabaaaaccaaaaba
    <br/> Output: a3ba4c2a4ba
    <br/>
    <br/> String Given: abcd
    <br/> Output: abcd
    <br/>
  </P>
</div>
<br/>
<p>Your Results:</p>
<div id="results3"></div>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/><br/>
<br/>
<br/><br/>
<br/>

<br/>
<br/>
<br/><br/>
<br/>
<br/><br/>
<br/>
<br/>
<br/>
<br/><br/>
<br/>
<br/><br/>
<br/>
<br/>
<br/>
<br/><br/>
<br/>
<br/><br/>
<br/>
<br/>
<br/>

CSS

p {
  padding: 0px 0px 0px 15px;
}

#question {
  border: 1px solid gray;
}

div {
  padding: 15px 15px 15px 15px;
  border: 1px solid blue;
}

JavaScript

//You may use: https://developer.mozilla.org if you need help with syntax
// https://getfirebug.com/firebug-lite-debug.js as external resource for a build-in console window
function Q1(number1, number2) {
  var returnValue = '';
  //Q1 your code here
  for (let num = number1; num <= number2; num++) {
    if (num === 2) {
      returnValue += ' ' + num;
    } else if (num % 2) {
      if (checkPrime(num)) {
        returnValue += ' ' + num;
      }    
    }
  }
  
  return returnValue;
}

function checkPrime(num) {
  if (num === 1) {
    return false;
  } else if (num === 3 || num === 5 || num === 7) {
    return true;
  } else if (num % 3 === 0) {
    return false;
  } else {
    const biggestFactor = Math.floor(Math.pow(num, 0.5));
    let factor = 5;
    // check factors that are multiples of 5 or 7
    // increment factors by 6
    while (factor <= biggestFactor) {
      if ( (num % factor) === 0) {
        return false;
      } else if ( (num % (factor+2)) === 0) {
        return false;
      }
      factor += 6;
    }
    return true;
  }
}

function Q2(number1) {
  var returnValue = "";

  //Q2 your code here

  return returnValue;
}

function Q3(string1) {
  var returnValue = "";
  
  //Q3 your code here
  
  // loop through the string
  for (let i = 0; i < string1.length; i++) {
    if (i === 0) {
      returnValue = _Q3(string1, i, returnValue)
    } else if (string1[i-1] && string1[i] !== string1[i-1]) {
      returnValue = _Q3(string1, i, returnValue)
    }
  }

  return returnValue;
}

function _Q3(string1, i, returnValue) {
    // check number of repeats (do this on slicing the string, plus the character, as parameters)
  let repeats = checkRepeats(string1.slice(i), string1[i]);
  // if it's more than one, we add the character plus the repeats
      
  if (repeats > 1) {
    returnValue += string1[i] + repeats;
  } else {
    // else if it's just one character, we add the character
    returnValue += string1[i];
  }
  
  return...