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 trial

JavaScript Object-Oriented JavaScript (2015) Introduction to Methods Returning Values

If 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);
}

It'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>

Thanks 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.

I 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
Steven Parker
229,785 Points

:point_right: dice.Roll was never defined. It is not a function.

But dice.roll (with lower-case "r") is.

Remember that JavaScript is case-sensitive.

oh yeah, now I see it.

Kristian Gausel
Kristian Gausel
14,661 Points

Try:

button.onclick = function(){
    var result = dice.roll();
    print(result);
}

JavaScript is case sensitive, and you're doing it right in the "print" example ;)

I copy and pasted yours below mine. They look exactly the same to me but yours works. What am I not seeing?

Kristian Gausel
Kristian Gausel
14,661 Points

the lowercase 'r' in 'dice.roll'

Steven Parker
Steven Parker
229,785 Points

"R" vs. "r"

(Uppercase vs. lowercase.)