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

My Solution: Roll for Initiative.

Had a lot of fun with this challenge. Love how Treehouse teaches how to breakdown your code step by step using console.log. Was having a rough time at the beginning with changing my string to a number and remembered we learned the "typeof" special operator. This is the first time I have ever learned that and I have been through 3 bootcamps. Treehouse I couldn't recommend you enough! Anyway, enough gushing ^__^ here is my solution:

// Collect input from a user

const userRoll = prompt(`Roll for iniative! Please provide a number`);


// Convert the input to a number
let user = parseInt(userRoll);

// Use Math.random() and the user's number to generate a random number
const randomNum = Math.floor ( Math.random() * user) +1;

// Create a message displaying the random number
const main = document.querySelector('main');
main.innerHTML = `<h2>You rolled a ${randomNum} out of ${user} die.</h2>`;

This is the index.html I adjusted for fun.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JavaScript Numbers – Challenge</title>
    <link href="css/style.css" rel="stylesheet"> 
  </head>
  <body>
    <h1>A Monster is attacking!! 👾</h1>
    <main></main>
    <script src="js/random.js"></script>   
  </body>
</html>

1 Answer