/    Sign up×
Community /Pin to ProfileBookmark

Javascript: Help with simple number Game

Hi everyone,

Firstly, forgive me i am new to Javascript….this seem simple and trivial to you.
I am trying to create a guess the number game that increments and displays the number of guesses each time the user inputs a number and presses the button. The number to be guess is random generated and i am looking to create an alert that notifies the user whether their guess is too low, high, or correct.

So far i have managed everything but the alert boxes for some reason i simply cannot get them to work!! I will give you the code of what i have written.

This is the HTML file:
form name=”gameForm” onload=’getInitialisation();’>
<h1> Guess My Number Game</h2>

<!– Input Boxes–>
<h3> Name: <input name=”name” type=”text” onchange=’checkName( this.value );’></h3>
<h3> Number: <input name=”number” type=”text” onchange=’checkNumber( this.value );’></h3>

<!–Counter–>
<h3>Number of guesses so far:
<span id=”counter”>0</span>
</h3>

<!–Action Buttons–>
<input type=”button” value=”Start Again”
onclick=’location.reload();’>

<input name=”guess” type=”button” value=”Guess”
onclick = ‘updateCounter();’>

<input type=”reset” value=”Clear”>
&nbsp; &nbsp;

</form>

and the css: (Ignore the get the date function and validation)

var countNo = 0; // number of guesses
var n; //number to be guessed

function Initialisation () {
countNo = 0;
n = Math.floor( 1 + 1000 * Math.random() );
}

function updateCounter() {
countNo++; // Increment countNo by 1
// Update the number of clicks so far
document.getElementById(“counter”).innerHTML =countNo;

var number = document.gameForm.guess.value

if (number<n)
{
alert(number + ” is too small, Guess again.”);
}

if (number>n)
{
alert( number +” is too big, guess again.”);
}

if (number==” “)
{
alert( ” You haven’t entered anything yet!”);
}

if (number==n && countNo==10)
{
alert( “well done +name+ Ahah! You know the secret!” );
}

if (number==n && countNo <10)
{
alert( “well done” +name+ “Either you know the secret or you got lucky!” );
}

if (number==n && countNo >10)
{
alert( “well done” +name+ “You should do better next time! );
}

}

function getFullDate() {
var today = new Date ();
var days = [“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”];
var months = [“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”];

var days = days[today.getDay()];
var day = today.getDate ();
var month = months[today.getMonth()];
var year = today.getFullYear();
return days + ” ” + day + ” ” + month + ” ” + year;
}

function checkName(name)
{
var invalid = /[^a-zA-Z]/;

if ( invalid.test(name) ) {
alert( name + ” is not a valid name.”);
}
}

function checkNumber(str)
{
number = parseInt(str);
if ( isNaN(number) || number < 1 || number > 1000 ) {
alert( number + ” is not a valid number.”);
}
}

Should i create a separate function and call both onclick? It also seems when i try to add the if staments all the other javascript stops working. Am i doing something stupidly obvious. Help would be greatly appreciated.

Thank you

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @EvangelineDodd spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...