Find the smallest and largest value in an array with JavaScript
<p>I am trying to write an algorithm that finds and smallest and largest value in an array, and the second largest and second smallest.</p>

<p>I tried with the following: </p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>numbers = [2, 4, 9, 2, 0, 16, 24]

var largest = numbers[0];
var smallest = numbers[0];

for (var i = 1; i &lt; numbers.length; i++) {

  if (numbers[i] &gt; largest) {
    largest = numbers[i];
  } else if (numbers[i] &lt; smallest) {
    smallest = numbers[i];
  }

  console.log(largest);
  console.log(smallest);
}</code></pre>
</div>
</div>
</p>

<p>This does not seem to work and just prints out the array...what am I doing wrong?  </p>