Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialjohn larson
16,594 PointsIf I use button for dice.roll() i get error dice.roll is not a function. If I print dice directly to the page it's ok
Directly to the page this works fine
function print(output){
document.getElementById("display").innerHTML = output;
}
var button = document.getElementById("button");
var dice = {
sides: 6,
roll: function(){
var randomNumber = Math.floor(Math.random() * this.sides) + 1;
return randomNumber;
}
}
print(dice.roll());
If I use this button I get the error dice.roll is not a function
button.onclick = function(){
var result = dice.Roll();
print(result);
}
john larson
16,594 PointsThanks to both of you. You guys were neck and neck on that one. If it would have been a horse race it would have been a photo finish.
john larson
16,594 PointsI must remind myself of the three S's: Syntax, Spelling and caseSensitive. It's often one of those three that catch me up.
2 Answers
Steven Parker
231,268 Pointsdice.Roll was never defined. It is not a function.
But dice.roll (with lower-case "r") is.
Remember that JavaScript is case-sensitive.
john larson
16,594 Pointsoh yeah, now I see it.
Kristian Gausel
14,661 PointsTry:
button.onclick = function(){
var result = dice.roll();
print(result);
}
JavaScript is case sensitive, and you're doing it right in the "print" example ;)
john larson
16,594 PointsI copy and pasted yours below mine. They look exactly the same to me but yours works. What am I not seeing?
Kristian Gausel
14,661 Pointsthe lowercase 'r' in 'dice.roll'
Steven Parker
231,268 Points"R" vs. "r"
(Uppercase vs. lowercase.)
john larson
16,594 Pointsjohn larson
16,594 PointsIt's when I click on the button that I get the error. So I gather that means the problem is in the function/onclick event itself...but I don't see it.
<button id="button">Roll the Dice</button>