// library.js
// created by Peter Hewitt 2020

// returns a random number between 1 and n inclusive
function random(n) {
  return Math.floor(Math.random() * (n) + 1);
}

// returns a random number between 0 and n inclusive
function random0(n) {
  return random(n+1)-1;
}

// Sort function (capital "S" to distingush from JavaScript sort)
// an array is sorted in place
//    Sort(array);
// a string is returned as a sorted string
//    new_string = Sort(string);
function Sort(obj) {
  if (typeof obj == 'object') { // assume it is an array
    if (typeof obj[0] == 'number') {
      obj = obj.sort((a, b) => a - b);
    } else {
      obj = obj.sort();
    }
  } else { // assume a string
    return obj.split('').sort().join('');
  }
}

// shuffle the array in place
function shuffle(array) {
  var i,j,temp;
  for (i=array.length-1; i>0; i-=1) {
    j=Math.floor(Math.random()*(i+1));
    temp=array[i]; array[i]=array[j]; array[j]=temp;
  }
}

// hide an element given its id
function hide(id) {
  var el=document.getElementById(id);
  el.style.visibility='hidden';
}

// show an element given its id
function show(id) {
  var el=document.getElementById(id);
  el.style.visibility='visible';
}

// hide elements given their class
function hide_class(klass) {
  var arr=document.getElementsByClassName(klass);
  for (var i=0;i<arr.length;i++) {
    var el=arr[i];
    el.style.visibility='hidden';
  }
}

// show elements given their class
function show_class(klass) {
  var arr=document.getElementsByClassName(klass);
  for (var i=0;i<arr.length;i++) {
    var el=arr[i];
    el.style.visibility='visible';
  }
}
