how to make a random number guessing game in javascript
JavaScript
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Guess the number</title>
<style>
p{
color: rgb(3, 68, 46);
}
.inputBox{
font-family: Arial, Helvetica, sans-serif
}
.button{
background-color: black;
color: blueviolet;
display: inline-block;
font-family: sans-serif;
padding: 7px;
border: blueviolet;
border-radius: 10px;
}
</style>
</head>
<body>
<p>I am thinking of a number between 1 and 100. Try and guess it.</p>
<div class="form">
<label for="guessField"><p>Enter a guess: </p></label>
<input type = "text" id="inputField" class = "inputBox">
<input type = "submit" value = "Submit guess"
class = "button" id="submitbutton">
<p id="output">Guess a number from 1 to 100!</p>
</div>
<script>
var magicnumber = Math.floor(Math.random() * 100 + 1);
var guesses = 1
document.getElementById("submitbutton").onclick = function(){
var playerguess = document.getElementById("inputField").value;
if playerguess == magicnumber{
if guesses == 1{
document.getElementById("output").innerHTML = "Well done! You guessed my number in" + guesses + "guess! Awsome!"
}
else{
document.getElementById("output").innerHTML = "Well done! You guessed my number in " + guesses + " guesses!"
}
if playerguess > magicnumber{
document.getElementById("output").innerHTML = "oops! " + playerguess + " is too high! Try a lower number"
guesses++
}
if playerguess < magicnumber{
document.getElementById("output").innerHTML = "oops! " + playerguess + " is too low! Try a higher number"
guesses++
}
}
</script>
</body>
</html>var userNumber =10;
var counter = 0;
var maxTries = 5;
var randomNumber = Math.floor(Math.random() * userNumber + 1);
while (attempts != randomNumber){
var attempts = prompt("Please pick a number between 1 and " + userNumber);
counter += 1;
if (counter > maxTries){
document.write("You have no more tries left. Press F5 to play again");
break
}
if (attempts == randomNumber){
document.write("Congrates you guess the correct number");
document.write(" the random number was " + randomNumber);
document.write(" It took you " + counter + " attempts to guess the correct number")
}
}
Also in JavaScript:
- Title
- how to use text onclick to display images in javascript
- Category
- JavaScript
- Title
- ionic (Emitted value instead of an instance of Error
- Category
- JavaScript
- Title
- append to array js
- Category
- JavaScript
- Title
- is checked checkbox jquery
- Category
- JavaScript
- Title
- axios delete request payload
- Category
- JavaScript
- Title
- angular submit form programmatically
- Category
- JavaScript
- Title
- how to make a factorial function in javascript
- Category
- JavaScript
- Title
- connect nodejs to mariadb
- Category
- JavaScript
- Title
- add new items in a select input using js
- Category
- JavaScript
- Title
- add image in react native
- Category
- JavaScript
- Title
- 1 - - 1 in javascript
- Category
- JavaScript
- Title
- app listen with all error handling
- Category
- JavaScript
- Title
- chrome extension get current tab from popup
- Category
- JavaScript
- Title
- add element to array javascript
- Category
- JavaScript
- Title
- greater than or equal to javascript
- Category
- JavaScript
- Title
- how to Write a program that simulates a coin toss using random method of Javascript Math class
- Category
- JavaScript
- Title
- add char in specific index stirng javascript
- Category
- JavaScript
- Title
- accept 2 values after decimal in angular forms
- Category
- JavaScript
- Title
- call function javascript
- Category
- JavaScript
- Title
- get last element of array javascript
- Category
- JavaScript
- Title
- brute force search javascript
- Category
- JavaScript
- Title
- get a element using name in jquery
- Category
- JavaScript
- Title
- angular observable subscribe and unsubscribe
- Category
- JavaScript
- Title
- compare dates in js
- Category
- JavaScript
- Title
- export aab react native
- Category
- JavaScript
- Title
- group by in javascript
- Category
- JavaScript
- Title
- change select value jquery
- Category
- JavaScript
- Title
- how to create a variable in javascript
- Category
- JavaScript
- Title
- fixed header on scroll vuejs
- Category
- JavaScript
- Title
- git remote url change
- Category
- JavaScript
- Title
- fetch a json from url python
- Category
- JavaScript
- Title
- get today's date javascript
- Category
- JavaScript
- Title
- how to push object in array using for loop javascript
- Category
- JavaScript
- Title
- how to add button in alert box in react native
- Category
- JavaScript
- Title
- bootstrap datepicker js cdn
- Category
- JavaScript
- Title
- How to pass a map from controller to javascript function in VF page
- Category
- JavaScript
- Title
- discord.js start code
- Category
- JavaScript
- Title
- .change() in pure js
- Category
- JavaScript
- Title
- generate new component angular
- Category
- JavaScript
- Title
- how to manage a db connection in javascript
- Category
- JavaScript
- Title
- faker js
- Category
- JavaScript
- Title
- change p tag text javascript
- Category
- JavaScript
- Title
- elasticsearch test node.js
- Category
- JavaScript
- Title
- ajax open a request
- Category
- JavaScript
- Title
- get current time epoch javascript
- Category
- JavaScript
- Title
- how to remove item from array javascript
- Category
- JavaScript
- Title
- async await promise all javascript
- Category
- JavaScript
- Title
- how to remove a property from an object in javascript
- Category
- JavaScript
- Title
- execute js with innerhtml
- Category
- JavaScript
- Title
- dumpdata json model dajngo
- Category
- JavaScript
- Title
- angular moment pipe timeAgo
- Category
- JavaScript
- Title
- arrow function in javascript w3schools
- Category
- JavaScript
- Title
- Date gethours js
- Category
- JavaScript
- Title
- how to add javascript in html
- Category
- JavaScript
- Title
- binary search in js
- Category
- JavaScript
- Title
- get x, y of element js
- Category
- JavaScript
- Title
- how to expire a cookie in javascript
- Category
- JavaScript
- Title
- get react version
- Category
- JavaScript
- Title
- how to instal jquery with npm
- Category
- JavaScript
- Title
- How to create a nested schema in apollo graphql server
- Category
- JavaScript
- Title
- how to import jquery in js file
- Category
- JavaScript
- Title
- express url redirect
- Category
- JavaScript
- Title
- javascript array stackoverflow
- Category
- JavaScript
- Title
- javascript array concat spread operator
- Category
- JavaScript
- Title
- angular 6 reload current page
- Category
- JavaScript
- Title
- download image in nodejs
- Category
- JavaScript
- Title
- array to string javascript
- Category
- JavaScript
- Title
- express route parameters
- Category
- JavaScript
- Title
- how to import npm packages in node.js?
- Category
- JavaScript
- Title
- angular get name of component
- Category
- JavaScript
- Title
- how to change node version
- Category
- JavaScript
- Title
- hwo to cehck req header in js
- Category
- JavaScript
- Title
- conditional jsx property
- Category
- JavaScript
- Title
- how to appendChild in the begin of the div javascript
- Category
- JavaScript
- Title
- angular material multi select ngmodel
- Category
- JavaScript
- Title
- how to parse query string in node.js lambda
- Category
- JavaScript
- Title
- javascript ascii to hex
- Category
- JavaScript
- Title
- closure javascript
- Category
- JavaScript
- Title
- get date javascript
- Category
- JavaScript
- Title
- get utc time javascript
- Category
- JavaScript
- Title
- chrome add a bookmark that appends to current url
- Category
- JavaScript
- Title
- change class javascript
- Category
- JavaScript
- Title
- How do I get Month and Date of JavaScript in 2 digit format
- Category
- JavaScript
- Title
- javascript addeventlistener
- Category
- JavaScript
- Title
- javascript class inheritance
- Category
- JavaScript
- Title
- dynamic forms in react
- Category
- JavaScript
- Title
- how to change a string to number in javascript
- Category
- JavaScript
- Title
- find class using jquery
- Category
- JavaScript
- Title
- javascript async fetch file html
- Category
- JavaScript
- Title
- How to insert an item into an array at a specific index JavaScript
- Category
- JavaScript
- Title
- deploy angular app on google app engine
- Category
- JavaScript
- Title
- getFullYear within moment in angular
- Category
- JavaScript
- Title
- generate bearer token
- Category
- JavaScript
- Title
- get screen resolution jquery
- Category
- JavaScript
- Title
- is javascript object oriented
- Category
- JavaScript
- Title
- angular date formats
- Category
- JavaScript
- Title
- delete previous line node
- Category
- JavaScript
- Title
- dom get all tags
- Category
- JavaScript
- Title
- how to make a circle in js
- Category
- JavaScript
- Title
- get window size javascript
- Category
- JavaScript