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

Advanced Random Number Generator Challenge Solution

This is not really a question, but rather a post of my code from my own personal random number generator. This took me quite awhile to work out. As I learned more I came back and started adding more to it. It is still pretty primitive, as I am still in the very early stages of learning JS, but I figured that my solutions here may help others.

I tried to find solutions to problems, such as the user entering non-numeric characters or answers outside of the scope or range of desired inputs. I realize this may not be the most elegant solution to these problems, but I am hampered by the fact that I don't know much JS at this moment. I'm sure my code will improve as I advance.

Below is the code for my solution: HTML

<html>

<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="css/main.css">
    <title>Random Number Generator</title>
    <meta charset="UTF-8" />
</head>

<body>
  <div id="start"></div>
  <script src="random.js"></script>
</body>

</html>

Below is the code for my solution: JavaScript

//Variables to use
let
name,
greeting,
yesNo,
numb1,
entNumb,
numb2,
randNumb,
message,
playAgain;

function startGame() {
//Welcomes user and prompts for input
name = prompt("Hey there! What's your name?");
greeting = prompt("Nice to meet you " + name + " ! Would you like to play a game?");

//Loop prompts only Yes or No answer
while (greeting.toUpperCase() !== "YES" && greeting.toUpperCase() !== "NO") {
    yesNo = prompt("I'm sorry. Please enter either 'yes' or 'no' to continue.");
    greeting = yesNo;
}

//Conditional based on Yes or No answer
if (greeting.toUpperCase() === "YES") {
  alert("Great! I'm going to ask your to enter two numbers. Press OK to continue");
} else if (greeting.toUpperCase() === "NO") {
  alert("Okay " + name + ", that's fine. Simply refresh this screen if you change your mind. Goodbye!");
}

//Collects numbers for random generation
numb1 = parseInt(Math.round(prompt("Please enter any number between 0 and 50.")));
while (isNaN(numb1) || numb1 < 0 || numb1 > 50) {
  entNumb = prompt("I'm sorry. Please enter a number between 0 and 50.")
  numb1=entNumb;
}
numb2 = parseInt(Math.round(prompt("Great! Now enter any number between 51 and 100.")));
while (isNaN(numb2) || numb2 < 51 || numb2 > 100) {
  entNumb = prompt("I'm sorry. Please enter a number between 51 and 100.")
  numb2=entNumb;
}

//Generates random number between first and second selected numbers
alert("Great! Now we're going to generate a random number between " + parseInt(numb1) + " and " + parseInt(numb2) + "! Press OK to continue.")

randNumb = Math.floor(Math.random() * (numb2 - numb1 + 1)) + numb1;
message = "Your random number between " + numb1 + " and " + numb2 + " is " + randNumb + "!";
alert(message);

//Creates button for playing game again
//Calls <div> element with id="start" from index.html
document.getElementById("start").innerHTML = `
<h1>Let's do that again!</h1>
<div>
  <button onclick="startGame()">Play Again</button>
</div>
`;
}

//Creates button for starting game
//Calls <div> element with id="start" from index.html
document.getElementById("start").innerHTML = `
<h1>Let's get this party started!</h1>
<div>
  <button onclick="startGame()">Start Game</button>
</div>
`;

Again, this may be a rough solution to problems that weren't necessarily possed in the original challenge, but they were issues I ran into myself and I hope they help others. I would love to see more solutions to similar problems if anybody has any.

1 Answer

Hey, Daniel ! I think this is a great solution ! The idea of adding a button for restarting the game is pretty cool ! Well done :)!

Thanks Mino. I'm really loving JS. It's a fun language for sure.