0% found this document useful (0 votes)
2 views3 pages

987654321 (2)

This JavaScript code implements a number guessing game where users generate a range of numbers based on their input and attempt to guess a secret number. Players score points for correct guesses and lose points for incorrect ones, with a win condition set at a score of 5 or higher. The game dynamically generates new ranges and updates the score accordingly, providing feedback through a text area on the interface.

Uploaded by

mickeygoose7140
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

987654321 (2)

This JavaScript code implements a number guessing game where users generate a range of numbers based on their input and attempt to guess a secret number. Players score points for correct guesses and lose points for incorrect ones, with a win condition set at a score of 5 or higher. The game dynamically generates new ranges and updates the score accordingly, providing feedback through a text area on the interface.

Uploaded by

mickeygoose7140
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

JavaScript

var min, max; //Lowest range and highest range


var numberList = []; //Blank list for the numbers to guess from
var secretNumber; //Number that you have to guess from
var score = 0; //Initial score

// Instructions for input


// User first must type a number in the generate range text box.
//Press "Generate numbers"
// The range text box will generate a range based on your number.
// You will have a secret number to guess from and will enter it in the bottom
textbox.
// Press "check" to check your guess.
// You will get a score and if the score is greater than 5, you win.

//Output: If you get the right guess, increases score by one and if you get it
wrong, decerases score by 1.
//If the score is greater than 5, it says you win.
//if score is less than 5, then the program says you lose.
//Generates a range from a number you input and displays on screen.
//BaseNumber allows you to add a number of your choice and turns that number
into a range
function chooseRandomRange(baseNumber) {
min = baseNumber;
max = min+4;

// Call generateNumberList to populate the numberList


generateNumberList();

generateSecretNumber();

return "New range: " + min + " to " + max + ". Guess the number!!!";
}

// Function to generate the number list for the range


function generateNumberList() {
numberList = [];
for (var i = min; i <= max; i++) {
appendItem(numberList, i);
}
}

// Function to generate the secret number randomly from the numberList


function generateSecretNumber() {
// Input: No parameters. Uses the global variable numberList.
// Output: Updates the global variable secretNumber with a randomly selected
number from numberList.
secretNumber = numberList[randomNumber(0, numberList.length - 1)];

// Update the text area to display the guessing range


setText("text_area1", "Guess a number in range: " + min + " to " + max);
}

// This function's parameter allows you to check a number.


function checkGuess(number) {
var found = false; //Initial condition.
var guess = number;

for (var i = 0; i < numberList.length; i++) {


if (numberList[i] == guess) {
found = true;
break;
}
}

if (!found) {
setText("text_area1", "Out of the range! Pick from: " + numberList.join(",
"));
return;
}

if (guess == secretNumber) {
score++;
setText("text_area1", "Correct! Score: " + score);
chooseRandomRange(min); //Generates another range
} else {
score--;
setText("text_area1", "Wrong! Try again! Score: " + score);
}

if (score >= 5) {
setText("text_area1", "You win!");
} else if (score <= -5) {
setText("text_area1", "You lose! The number actually was: " +
secretNumber);
score = 0;
}
}
// Event handler for "guessButton"
onEvent("guessButton", "click", function() {
checkGuess(getNumber("text_input1"));
});

// Event handler for "rangeButton"


onEvent("rangeButton", "click", function() {
setText("text_area1", chooseRandomRange(getText("rangeInput")));
});

You might also like