Ad
How Do I Decrement The Value In My Java Script Code?
The code below is for "guess the number game" in a web page where the user is the one thinking of the number and the Computer is to guess the number that users are thinking. So I need to know how to automatically decrease the value (number) in the "Guesses Remaining" input field.
<html>
<head>
<meta charset="UTF-8">
<script src="lab1a.js"></script>
</head>
<body>
Guess a number game: <br/> <br/>
Upper Bound: <input id="UPPER" type="text"></input> <br/>
Lower Bound: <input id="LOWER" type="text"></input> <br/>
Guesses Remaining: <input id="REMAINING" type="text"></input> <br/>
<button onclick="play()">Play!</button> <br/>
Computer's Guess: <span id="COMP_GUESS"></span> <br/>
<button onclick='high("GUESS_HIGHER")'>Guess Higher</button>
<button onclick="low('GUESS_LOWER')">Guess Lower</button>
<button disabled=true onclick="correct()" id="CORRECT">Correct!!!</button>
</body>
</html>
Here is the javascript code:
function getFieldValue(target) {
var elm = document.getElementById(target);
var val = parseInt(elm.value);
return val;
}
function getCompGuess() {
var upper = getFieldValue("UPPER");
var lower = getFieldValue("LOWER");
return (parseInt((upper + lower) / 2))
}
/* User starts game. */
function play() {
document.getElementById('CORRECT').removeAttribute("disabled");
var remaining = getFieldValue("REMAINING");
var compGuess = getCompGuess();
var compElm = document.getElementById("COMP_GUESS");
compElm.innerHTML = compGuess;
document.compGuess = compGuess;
}
/* "Correct" Button pressed. */
function correct() {
var r = getFieldValue("REMAINING");
alert("Computer wins with " + r + " guesses remaining.");
}
/* Higher or lower button */
function high(userChoice) {
if (userChoice === "GUESS_HIGHER")
var low = getFieldValue("LOWER");
if (document.compGuess > low) {
document.getElementById("LOWER").value =
document.compGuess;
}
play();
}
function low(userChoice) {
if (userChoice === "GUESS_LOWER")
var high = getFieldValue("UPPER");
if (document.compGuess < high) {
document.getElementById("UPPER").value = document.compGuess;
}
play();
}
Ad
Answer
This is the answer for your question "So I need to know how to automatically decrease the value (number) in the "Guesses Remaining" input field."
function getFieldValue(target) {
var elm = document.getElementById(target).value;
var val = parseInt(elm);
return val;
}
function getCompGuess() {
var upper = getFieldValue("UPPER");
var lower = getFieldValue("LOWER");
return (parseInt((upper + lower) / 2))
}
/* User starts game. */
function play() {
var guess_remaining = document.getElementById("REMAINING").value;
document.getElementById('CORRECT').removeAttribute("disabled");
var remaining = getFieldValue("REMAINING");
if(isNaN(remaining)){ //When you first call getFieldValue("REMAINING") it returns NaN, means user just started to play
var guess_remaining = 5;
document.getElementById("REMAINING").value = guess_remaining;
}
else {
if(guess_remaining>0) {
guess_remaining = guess_remaining-1;
var compGuess = getCompGuess();
var compElm = document.getElementById("COMP_GUESS");
compElm.innerHTML = compGuess;
document.compGuess = compGuess;
}
document.getElementById("REMAINING").value = guess_remaining;
}
}
/* "Correct" Button pressed. */
function correct() {
var r = getFieldValue("REMAINING");
alert("Computer wins with " + r + " guesses remaining.");
}
/* Higher or lower button */
function high(userChoice) {
if (userChoice === "GUESS_HIGHER")
var low = getFieldValue("LOWER");
if (document.compGuess > low) {
document.getElementById("LOWER").value =
document.compGuess;
}
play();
}
function low(userChoice) {
if (userChoice === "GUESS_LOWER")
var high = getFieldValue("UPPER");
if (document.compGuess < high) {
document.getElementById("UPPER").value = document.compGuess;
}
play();
}
Guess a number game: <br/> <br/>
Upper Bound: <input id="UPPER" type="text"></input> <br/>
Lower Bound: <input id="LOWER" type="text"></input> <br/>
Guesses Remaining: <input id="REMAINING" type="text"></input> <br/>
<button onclick="play()">Play!</button> <br/>
Computer's Guess: <span id="COMP_GUESS"></span> <br/>
<button onclick='high("GUESS_HIGHER")'>Guess Higher</button>
<button onclick="low('GUESS_LOWER')">Guess Lower</button>
<button disabled=true onclick="correct()" id="CORRECT">Correct!!!</button>
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad