Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Conditional statement for an array

I am trying to put together a little craps program. I am just experimenting. The issue I am having is I am putting together a conditional statement for when snake eyes hits (1, 1).....am I referring the actual value of 1 or its index in the array? Trying to get markdown correctly sense I'm new to post code in here.

'''

const die1 = [1,2,3,4,5,6]; const die2 = [1,2,3,4,5,6];

if (die1 === 1 && die2 === 1){ 'alert('Snake eyes!'); } const diceRoll1 = die1[Math.floor(Math.random()*die1.length)]; const diceRoll2 = die2[Math.floor(Math.random()*die2.length)];

document.write(diceRoll1 + ' ' + diceRoll2);

'''

3 Answers

Hi Curtis

Firstly, if you would like to inline JavaScript in HTML you need wrap a script tag around it like so

<p>
    <script type="text/javascript">
        const die1 = [1,2,3,4,5,6]; const die2 = [1,2,3,4,5,6];

        if (die1 === 1 && die2 === 1){ alert('Snake eyes!'); } const diceRoll1 = die1[Math.floor(Math.random()die1.length)]; const diceRoll2 = die2[Math.floor(Math.random()die2.length)];

        document.write(diceRoll1 + ' ' + diceRoll2);
    </script>
</p>

Secondly, to get a random item from an array you need to multiply the random number with the item.length like so

<p>
    <script type="text/javascript">
        const die1 = [1,2,3,4,5,6]; 
        const die2 = [1,2,3,4,5,6];

        if (die1 === 1 && die2 === 1){ 
            alert('Snake eyes!');
        } 

        const diceRoll1 = die1[Math.floor(Math.random()*die1.length)];
        const diceRoll2 = die2[Math.floor(Math.random()*die2.length)];

        document.write(diceRoll1 + ' ' + diceRoll2);
    </script>
</p>

Thirdly, die1 and die2 will never equal 1 because they are an array of numbers so you want to check if the diceroll1 and 2 are equal to 1 to get the Snake eyes.

So here is your working code:

I hope this makes sense, feel free to ask me any more questions

<p>
    <script type="text/javascript">
        const die1 = [1,2,3,4,5,6]; 
        const die2 = [1,2,3,4,5,6];

        const diceRoll1 = die1[Math.floor(Math.random()*die1.length)];
        const diceRoll2 = die2[Math.floor(Math.random()*die2.length)];

        if (diceRoll1 === 1 && diceRoll2 === 1){ 
            alert('Snake eyes!');
        } 

        document.write(diceRoll1 + ' ' + diceRoll2);
    </script>
</p>

Good Luck!

So I may need to set individual variables for the condition to work ?

Nevermind my previous response, I see it.