Your function roll
is returning your roll1 and roll2 before it gets to update the html. Upon being executed return roll1
exits the function and returns the value of role1. The lines of code after it never get called. What you need to do is just remove the returns from the function. when getElementById()
is called that will, in a way, act as you returning the variables because it will update the html to display your result.
let roll = function() {
let roll1;
let roll2;
roll1 = parseInt(Math.random() * 6) + 1;
roll2 = parseInt(Math.random() * 6) + 1;
document.getElementById('dice').innerHTML = roll1 + "and " + roll2;
}
0
solved Javascript function not working, what is wrong? [closed]