js function to calculate golf scores

this function will return the score name depending on the par and strokes for golf

Demo

Golf Score Calculator
Enter the Par Number
Enter the Number of Strokes

Test Results buttons

These buttons are for manually testing the function.

Code

function golf_score(par, strokes) {
  var score_names = ['a Hole-in-one!', 'an Eagle (2 Under Par)', 'a Birdie', 'on Par', 'a Bogey', 'a Double Bogey', '.. Go Home!'];
  if(strokes == 1) { // this is always a hole in 1
    return score_names[0]; // hole in one
  }
  if(strokes === par) {
    return score_names[3]; // on par
  }
  if(strokes <= par - 2) {
    return score_names[1]; // eagle
  }
  if(strokes == par - 1) {
    return score_names[2]; // birdie
  }
  if(strokes >= par + 3) {
    return score_names[6]; // go home
  }
  if(strokes >= par + 2) {
    return score_names[5]; // double bogie
  }
  if(strokes >= par + 1) {
    return score_names[4]; // bogie
  }
}
function calc_result(ParNum, StrokesNum) {
  if(typeof ParNum === 'undefined') {
    ParNum = document.getElementById('ParNum').value;
  }
  if(typeof StrokesNum === 'undefined') {
    StrokesNum = document.getElementById('StrokesNum').value;
  }
  score_name = golf_score(ParNum, StrokesNum);
  document.getElementById('score_results').innerHTML = 'Par ' + ParNum + ' with ' + StrokesNum + ' Stroke(s) is ' + score_name;
}
console.log(golf_score(par = 3, strokes = 1));
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

❤👩‍💻🕹