Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Random number guessing game - refactor
"use strict";
var upper = 10000,
numToGuess = getRandomNumber(upper),
compGuess,
numOfGuesses = 0;
function getRandomNumber(upper) {
return Math.floor( Math.random() * upper ) + 1;
}
while ( compGuess !== numToGuess ) {
compGuess = getRandomNumber(upper);
numOfGuesses++;
}
console.log('Hooray! The number was: ' + numToGuess + '. And the computer guessed: ' + compGuess);
console.log('It took ' + numOfGuesses + ' to get it right.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment