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 trialTyrel Kessinger
8,414 PointsAbsolutely Cannot Get Code to Run
No matter what I try, clearing the cache, re-starting my computer, running other browsers I CANNOT get the code for any of the Loops, Arrays and Objects to run in Workspace. I've pored over the code a million times and I'm not seeing anything that prevents this so any help would be appreciated (before I dropkick the laptop through the window!) Thanks for any help!
HTML side:
<!DOCTYPE html>
<html lang ="en">
<head>
<meta charset="UTF-8">
<title>Let's Make Random Numbers</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Let's Make Random Numbers</h1>
<script src="scripts.js"></script>
</body>
</html>
JavaScript code:
var randomNumber = getRandomNumber(10); var guess; var guessCount = 0; var correctGuess = false;
function getRandomNumber( upper ) { var num = Math.floor(Math.random() * upper) + 1; return num; }
do {
guess = prompt('I am thinking of a number between 1 and 10. What is it?');
guessCount += 1;
if (parseInt(guess) === randomNumber) {
correctGuess = true;
}
} while ( ! correctGuess )
document.write('<h1>You guessed the number!</h1>'); document.write('It took you ' + guessCount + ' tries to guess the number ' + randomNumber);
6 Answers
anil rahman
7,786 Points<!DOCTYPE html>
<html lang ="en">
<head>
<meta charset="UTF-8">
<title>Let's Make Random Numbers</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Let's Make Random Numbers</h1>
<script src="scripts.js"></script>
</body>
</html>
//JavaScript code:
var randomNumber = getRandomNumber(10);
var guess;
var guessCount = 0;
var correctGuess = false;
function getRandomNumber( upper )
{
var num = Math.floor(Math.random() * upper) + 1;
return num;
}
do
{
guess = prompt('I am thinking of a number between 1 and 10. What is it?');
guessCount += 1;
if (parseInt(guess) === randomNumber)
{
correctGuess = true;
}
} while ( ! correctGuess )
document.write('<h1>You guessed the number!</h1>');
document.write('It took you ' + guessCount + ' tries to guess the number ' + randomNumber);
Adam Brown
12,453 PointsYou have a ton of extra whitespace characters, especially in your HTML. I'm not sure if that's you or a result of copy/pasting carriage returns. It also looks like you're missing a semi-colon after the "while" statement at the end of your do/while loop.
Eric Thompson
9,858 PointsI am experiencing the same as well: the code does not run despite having written an exact mirror copy of what Dave has written. I have also experienced this with another lesson as well. If I remember correctly, I switched to Safari and it worked then (currently using Chrome).
Adam Brown
12,453 PointsEric Thompson Different browsers allow you to get away with different errors.
If I were you, I'd run it through a Javascript validator like JSLint. JSLint is EXTREMELY picky and will find errors in your code. Some of them (like for loops) are highlighted as errors even though they aren't actual errors.
If you're NOT using Workspaces, another good troubleshooting tool is to open up console on your browser. Ctrl+Alt+i for Windows, Cmd+Alt+i for Mac
Eric Thompson
9,858 PointsThanks Adam. I will definitely try your suggestions
Ayaz Hussein
13,348 PointsWhat do you mean by code not running? no prompt appears? if so, try to force refresh the browser, I had the same problem but it worked with a force refresh Windows: ctrl + F5 Mac/Apple: Apple + R or command + R Linux: F5
EDIT: just noticed, check the
<script src="scripts.js"></script>
as far as I know, on my workspace , it was in a folder named "js" when I did it so I think you have to add
<script src="js/scripts.js"></script>
Tyrel Kessinger
8,414 PointsThanks for the responses but neither of these will give me a successful preview of the code. As I've stated, I've cleared the cache countless times, restarted he computer several times and tried different browsers all to no avail
Ayaz Hussein
13,348 Pointsby previewing the code as in the prompt won't appear?
Tyrel Kessinger
8,414 PointsYes, when previewing the code. Nothing happens. Just a blank page in Chrome or whatver browser I use. It's becoming incredibly frustrating.
Ziv Peer
1,677 Pointsfor me prompt worked, problem is in: function getRandomNumber( upper ){}
from where u getting value for "upper"? Please check it.
anil rahman
7,786 Pointsanil rahman
7,786 Pointsformatted for people to read easily