switchMaxMin-Java Script

I have to write a function switchMaxMin(tab, n) that swaps the maximum element with the minimum element in an n-element array tab. We assume that all elements of the array are distinct (i. e. there are not a few maxima or minima). I don’t know how to do this
I started to write the code and I came up with this:

var tab = new Array(6, 4, 65, 34, 67, 89, 45, 7, 35, 79, 23, 56, 87, 12, 38, 9);
var min = tab[0];
var max = tab[0];
document.write("Tablica: ");
for (i = 1; i < tab.length; i++) {
  document.write(tab[i] + ", ");
  if (min > tab[i]) {
    min = tab[i];
  }
  if (max < tab[i]) {
    max = tab[i];
  }
}
document.write("<br /><br />Max: " + max);
document.write("<br />Min: " + min);

1 thought on “switchMaxMin-Java Script”

Leave a Comment