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 JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops What are Loops?

how to make this code DRY

// Random Number Generate User Number By uNumber or Defalut Number 6.  
function getRandomNumber(uNumber) {
    if (uNumber >= 1) {
        return Math.floor(Math.random() * uNumber) + 1;
    } else {
        //Default Random Number Generate By 6
        return Math.floor(Math.random() * 6) + 1;
    }

}
var randomNumberEmpty = getRandomNumber();
document.write("<p>" + "Random Number: " + randomNumberEmpty + "</P>");
//Random Number End Here.

//Randam Number Human Vs Computer Guess?
var upper = 10000;
var randomNumber = getRandomNumber(upper);
var guess;
var guessCount = 0;
while (guess != randomNumber) {
    guess = getRandomNumber(upper);
    guessCount += 1;
}
document.write("<p>" + "Computer Guess: " + randomNumber + "</p>");
document.write("<p>" + "Total Guess: " + guessCount + "</p>");
//Random Number Human Vs Computer Guess End Here
var mUpper = 10000;
var mRandomNumber = getRandomNumber(mUpper);
var mGuess;
var mGuessCount = 0;
var mCorrectGuess = false;
do {
    mGuess = prompt("Computer Vs You: Enter some Number: " + "Cheat Code: " + mRandomNumber);
    if (parseInt(guess) === randomNumber) {
        mCorrectGuess = true;
    }
    mGuessCount += 1;
} while (!mCorrectGuess)
document.write("<p>" + "Computer Guess Vs You: " + mRandomNumber + "</p>");
document.write("<p>" + "Your Guess Total Guess: " + mGuessCount + "</p>");

1 Answer

Hey Syalih,

I like your little program!

But there are actually a couple logic issues with your code. In your 2nd part of the program, where you are being prompted to guess what the computer randomized, you are not checking for the right variables. You should be checking mGuess and mRandonNumber because guess and randomNumber were used for the first part of the program.

There are also a couple things you can do to be more DRY. If you're using the same upper limit in both parts, you can just set upper and then call upper both times, instead of initializing two different variables. Also, your do-while statement doesn't require a variable in order to work correctly. You can just say "while(true)" and then issue a "break;" command once the if-statement goes off. I also put some strings together that don't need to be split apart in the prompt and document.write commands.

Here is my version of your code that utilizes a couple less variables:

// Random Number Generate User Number By uNumber or Defalut Number 6.  
function getRandomNumber(uNumber) {
    if (uNumber >= 1) {
        return Math.floor(Math.random() * uNumber) + 1;
    } else {
        //Default Random Number Generate By 6
        return Math.floor(Math.random() * 6) + 1;
    }

}
var randomNumberEmpty = getRandomNumber();
document.write("<p>Random Number: " + randomNumberEmpty + "</p>");
//Random Number End Here.

//Randam Number Human Vs Computer Guess?
var upper = 10000;
var randomNumber = getRandomNumber(upper);
var guess;
var guessCount = 0;
while (guess !== randomNumber) {
    guess = getRandomNumber(upper);
    guessCount += 1;
}
document.write("<p>Computer Guess: " + randomNumber + "</p>");
document.write("<p>Total Guess: " + guessCount + "</p>");
//Random Number Human Vs Computer Guess End Here
var mRandomNumber = getRandomNumber(upper);
var mGuess;
var mGuessCount = 0;
do {
    mGuess = prompt("Computer Vs You: Enter some number. Cheat Code: " + mRandomNumber);
    mGuessCount += 1;
    if (parseInt(mGuess) === mRandomNumber) {
        break;
    }
} while (true)
document.write("<p>Computer Guess Vs You: " + mRandomNumber + "</p>");
document.write("<p>Your Guess Total Guess: " + mGuessCount + "</p>");

Thank You :)

You are very welcome, Syalih! Happy Coding! :)