Getting results from a javascript function to display in html element.
I can get it to work with console.log(golfScore(4, 3)); but not user input.
I'm pretty sure the problem is in this line document.getElementById("message").innerHTML = golfScore();
If I put numbers or anything inside golfScore( HERE ); nothing works.
I can get console.log(golfScore(4, 3)) to print the correct answer in the console or I can get the correct answer to display in the p element if I use something like document.getElementById("message").innerHTML = golfScore(4, 3);
But I can't get the user input to run through the function and return the correct answer in the p element
// 2 user inputs one for par and one for strokes. depending on 2 values entered re
let strokes = document.getElementById('strokes').value;
let par = document.getElementById('par').value;
let names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) {
if (strokes == 1) {
return names[0];
} else if (strokes <= par - 2) {
return names[1];
} else if (strokes == par - 1) {
return names[2];
} else if (strokes === par) {
return names[3];
} else if (strokes == par + 1) {
return names[4];
} else if (strokes == par + 2) {
return names[5];
} else if (strokes >= par + 3) {
return names[6];
}
document.getElementById("message").innerHTML = golfScore();
}
<!DOCTYPE html>
<html>
<head>
<title>Golf Score</title>
</head>
<body>
<h1>Golf Score</h1>
<form>
Strokes: <input type="text" id="strokes"> Par: <input type="text" id="par">
<button type="button" onclick="golfScore(par, strokes)">How did you do?</button>
</form>
<p id="message"> </p>
</body>
</html>