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 JavaScript Basics (Retired) Working With Numbers Create a random number

I want to show images in DOM when the guessed number is not 2? here's my code?

var dieRoll = Math.floor(Math.random() * 6) + 1;

prompt(' guess the number from 1 to 5 and if you guessed the right number you won or if you not you loose');

if (dieRoll === 2) { alert('you guessed it.... hurray'); document.write(' You are good in guessing ' + dieRoll); } else { alert('looser looser looser looser');

};

3 Answers

Steven Parker
Steven Parker
231,269 Points

You'd probably benefit from the JavaScript and the DOM course, but here's one way you might do it:

yourpage.html
  <!-- keep hidden until code reveals it -->
  <img id="reveal" style="display:none" src="loserpic.jpg">
yourscript.js
//...
} else {
    alert('looser looser looser looser');
    document.getElementById("reveal").style.display = "";  // make image appear
};

Also, I notice that the entered value from the "prompt" isn't being used in the code, but the test for the value "2" is being done on a random number instead. Is that what you intended?

yes i was learning about math object.

thank you for helping me .

you could also to display from 0 to 6 this line of code: Math.floor(Math.random()*7);

you multiply by 7 you will still get the max answer 6 coz even 0.9999999 * by 7 will be 6.xxxxxx and with the Math.floor it always will down to the full number.