I am trying to make a random math quiz and I want to get new random numbers
after every click on the button.
I success to get new random numbers on every click but I still get the result of the first random numbers I got on the loading on the page
Another thing I need help to increase the score size-font every time the user enters the correct answer.
I tried to do it with 2.0em but it increases only once.
HTML
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<center>
<h1 style="font-size: 400%">Random Math Quiz</h1>
<div id="question"></div>
<input type="text" name="answer" id="answer" style="width: 250px; height: 40px; font-size: 32px;">
<br><br>
<button onclick="quiz()">Enter</button>
<br><br>
Score: <div id="score">0</div>
</center>
<script src="script.js"></script>
</body>
</html>
JavaScript
var int1 = Math.floor(Math.random() * 10) + 1;
var int2 = Math.floor(Math.random() * 10) + 1;
document.getElementById('question').innerHTML = int1 + " " + "+" + " " + int2;
var answer = int1 + int2;
var score=0;
function quiz() {
{
var uanswer = document.getElementById('answer').value;
if (uanswer == answer) {
alert("Correct");
score=(score)+1;
document.getElementById('score').innerHTML =score ;
document.getElementById("question").style.fontSize = "2.0em";
} else {
alert("OOPS Try Again!")
score= (score)-5;
document.getElementById('score').innerHTML =score ;
}
}
}
The reason your numbers do not change, is because every time you call that quiz function, you did not change the original int1 and int2. The whole javascript is only loaded once and and two numbers will only be set at a random number, once. Unless you fire a function to change the variables.
try this:
var score=0;
var int1 = Math.floor(Math.random() * 10) + 1
var int2 = Math.floor(Math.random() * 10) + 1
var answer = int1 + int2;
document.getElementById('question').innerHTML = int1 + " " + "+" + " " + int2;
function quiz() {
var user_answer = document.getElementById('answer').value;
if (user_answer == answer) {
alert("Correct");
score=(score)+1;
document.getElementById('score').innerHTML =score
} else {
alert("OOPS Try Again!")
score= (score)-5;
document.getElementById('score').innerHTML =score ;
}
// you need to reset the int1, int2, and answer by generating new numbers
int1 = Math.floor(Math.random() * 10) + 1;
int2 = Math.floor(Math.random() * 10) + 1;
answer = int1 + int2
document.getElementById('question').innerHTML = int1 + " " + "+" + " " + int2;
}
Since int1 and int2 are outside of the function they going to have the same value.
You need to generate a new random number each time you call the function quiz().
same with the font size - you changed it only once from default to 2em when you need to increase the value every time you call quiz().
var int1 = Math.floor(Math.random() * 10) + 1;
var int2 = Math.floor(Math.random() * 10) + 1;
document.getElementById('question').innerHTML = int1 + " " + "+" + " " + int2;
var answer = int1 + int2;
var score=0;
var fontSize = 1;
function quiz() {
{
var uanswer = document.getElementById('answer').value;
if (uanswer == answer) {
alert("Correct");
score=(score)+1;
document.getElementById('score').innerHTML =score ;
document.getElementById("question").style.fontSize = fontSize + 'em';
fontSize++;
int1 = Math.floor(Math.random() * 10) + 1;
int2 = Math.floor(Math.random() * 10) + 1;
answer = int1 + int2;
} else {
alert("OOPS Try Again!")
score= (score)-5;
document.getElementById('score').innerHTML =score ;
}
}
}
What about this approach? You call a function in order to reset the random values after each guess. And you store your fontSize in a variable, which you increase by 2 each time the user guesses right.
// intitial setup
var int1 = Math.floor(Math.random() * 10) + 1;
var int2 = Math.floor(Math.random() * 10) + 1;
document.getElementById('question').innerHTML = int1 + " " + "+" + " " + int2;
var answer = int1 + int2;
var score=0;
var fontSize = 2;
function quiz() {
var uanswer = document.getElementById('answer').value;
if (uanswer == answer) {
alert("Correct");
score=(score)+1;
document.getElementById('score').innerHTML =score;
document.getElementById("question").style.fontSize = fontSize + ".0em";
// increase fontSize by 2 for the next correct answer
fontSize += 2;
} else {
alert("OOPS Try Again!")
score= (score)-5;
document.getElementById('score').innerHTML =score;
}
// set new values
resetValues();
}
resetValues = () => {
int1 = Math.floor(Math.random() * 10) + 1;
int2 = Math.floor(Math.random() * 10) + 1;
document.getElementById('question').innerHTML = int1 + " " + "+" + " " + int2;
answer = int1 + int2;
score=0;
}
Related
function he() {
var x = Math.floor((Math.random() * 100) + 1);
document.getElementById("res1").innerHTML = "Heroes have dealt " + x + " damage on villains.";
var vhp = villains - x;
document.getElementById("res2").innerHTML = "<br/>Villains have " + vhp + " hp left.";
vok();
}
function vok() {
var y = Math.floor((Math.random() * 100) + 1);
document.getElementById("res3").innerHTML = "<br/>Villains have dealt " + y + " damage on Heroes.";
var hhp = hero - y;
document.getElementById("res4").innerHTML = "<br/>Heroes have " + hhp + " hp left.";
he();
}
Here is the above code I made function he() is called when a button is clicked and vok function is called after execution of he() function but it's not going good currently i want to overwrite the innerHTML every time the function is runned how am I supposed to do so?
I am working on creating a small 5 question addition quiz that asks the user a new random addition question every time they answer the previous one. I would like to be able to have my script ask a new question every time the user clicks on the "check answer" button without having to refresh the page. Then, once the user has completed the 5 questions, I would like to be able to have a popup state they have completed the quiz and can refresh the page to start a new quiz. With my current code, the script asks a random addition question, and then once the user clicks on the "check answer" button, the user must refresh the page to get a new question. I am stuck on figuring out how to modify my current code to fit what I am trying to do. I am thinking that a while loop may be they way to go, but I don't really know how to implement it. The code that I have so far is: `
<h1>Addition Quiz!</h1>
<p>This short 5 question quiz will test your addition skills! Answer the question as the computer asks it and pat yourself on the back for each correct answer!</p>
<h3 id="mathquestion"></h3>
<input type="text" name="answerbox" id="answerbox">
<button onclick="addition()">Check Answer</button>
<script>
var minimum = 1;
var maximum = 9;
var intiger1 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
var intiger2 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
document.getElementById("mathquestion").innerHTML = intiger1 + " " + "+" + " " + intiger2;
var questionanswer = intiger1 + intiger2;
function addition() {
var useranswer = document.getElementById('answerbox').value;
if (useranswer == questionanswer) {
alert("Correct! Congrats!");
} else {
alert("Sorry, your answer is incorrect. Better luck with the next question!")
}
}
</script>`
So what you want is the part that setups your question into a new function. And run that function when the answer is right. An example:
<script>
var minimum = 1;
var maximum = 9;
var questionanswer = 0;
setupQuestion();
function setupQuestion(){
var intiger1 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
var intiger2 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
document.getElementById("mathquestion").innerHTML = intiger1 + " " + "+" + " " + intiger2;
questionanswer = intiger1 + intiger2;
}
function addition() {
var useranswer = document.getElementById('answerbox').value;
if (useranswer == questionanswer) {
alert("Correct! Congrats!");
setupQuestion();
} else {
alert("Sorry, your answer is incorrect. Better luck with the next question!")
}
}
</script>`
Just update the variable
<script>
var minimum = 1;
var maximum = 9;
var intiger1 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
var intiger2 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
document.getElementById("mathquestion").innerHTML = intiger1 + " " + "+" + " " + intiger2;
var questionanswer = intiger1 + intiger2;
function addition() {
var useranswer = document.getElementById('answerbox').value;
if (useranswer == questionanswer) {
alert("Correct! Congrats!");
} else {
alert("Sorry, your answer is incorrect. Better luck with the next question!")
}
// Updating question
intiger1 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
intiger2 = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
document.getElementById("mathquestion").innerHTML = intiger1 + " " + "+" + " " + intiger2;
}
</script>
Simple button that should be creating new <p> elements when its clicked but it isn't working and I'm not sure why as I've compared it to other code of mine.
Demo
var battle = function() {
while(monsterHP > 0){
var playerDam = Math.floor(Math.random() * ((playerAtk - monsterAtk) + 2);
$('#battle').append("<p>You have hit the monster for " + playerDam + " damage. The monster has " + (monsterHP - playerDam) + "HP left</p>");
monsterHP -= playerDam;
if(monsterHP <= 0) {
$('#battle').append("<p>You have defeated the monster</p>");
}
}
}
$('#battleButton').click(function() {
battle();
}
You have many syntax errors in your code. If you correct them (as below), then it works fine. When you write a JQuery append, you need to put a ')' after your argument. Likewise, functions need to have a '}' after them.
var playerAtk = 5;
var playerDef = 5;
var playerHP = 10;
var monsterAtk = 4;
var monsterDef = 4;
var monsterHP = 8;
var battle = function() {
while(monsterHP > 0){
var playerDam = Math.floor(Math.random() * ((playerAtk - monsterAtk) + 2));
$('#battle').append("<p>You have hit the monster for " + playerDam + " damage. The monster has " + (monsterHP - playerDam) + "HP left</p>");
monsterHP -= playerDam;
if(monsterHP <= 0) {
$('#battle').append("<p>You have defeated the monster</p>");
}
}
}
$('#battleButton').click(function() {
battle();
});
For some reason the code inside the if statement at line 224 does not run. When the function loose() is called outside of the if statement it works. I am unsure as to what the issue with my code is.
<!DOCTYPE html>
<html>
<head>
<title>Jordan's Space Explorer </title>
</head>
<body>
<script>
// Tells user what they are about to play.
confirm ("You are about to play Jordan's Space Explorer?")
// Prompt user for their name.
userName="Capt. "+prompt("What is your name?");
shipName=prompt("What is your ship's name?")
// Declare start locations, default star system locations, and check for encounters.
var min = -10;
var max = 10;
var locationY = Math.floor(Math.random() * (max - min + 1)) + min;
var locationX = Math.floor(Math.random() * (max - min + 1)) + min;
var locationZ = Math.floor(Math.random() * (max - min + 1)) + min;
var locationYEast;
var locationYWest;
var locationXNorth;
var locationXSouth;
var locationZAbove;
var locationZBelow;
var solLocationX = Math.floor(Math.random() * (max - min + 1)) + min;
var solLocationY = Math.floor(Math.random() * (max - min + 1)) + min;
var solLocationZ = Math.floor(Math.random() * (max - min + 1)) + min;
var spacePortLocationX = Math.floor(Math.random() * (max - min + 1)) + min;
var spacePortLocationY = Math.floor(Math.random() * (max - min + 1)) + min;
var spacePortLocationZ = Math.floor(Math.random() * (max - min + 1)) + min;
var sensors = false;
var locationName = false;
var credits = 1000
var health = 100
var shipSpeed = 1
var lasers = 10
encounters();
locationQuery();
//Go directions
function goNorth(){
locationY = locationY +shipSpeed;
console.log("You head North.");
encounters();
console.log("Y = "+locationY);
}
function goSouth(){
locationY = locationY- shipSpeed;
console.log("You head South.");
encounters();
console.log("Y = "+locationY);
}
function goEast(){
locationX = locationX+shipSpeed;
console.log("You head East");
encounters();
console.log("X = "+ locationX);
}
function goWest(){
locationX = locationX-shipSpeed;
console.log("You head West");
encounters();
console.log("X = "+locationX);
}
function goUp(){
locationZ = locationZ +shipSpeed;
console.log("You go up.");
encounters();
console.log("Z = "+locationZ);
}
function goDown(){
locationZ = locationZ-shipSpeed;
console.log("You go down.");
encounters();
console.log("Z = "+locationZ);
}
// Encounters
function encounters()
{
// Sol
if(locationX === solLocationX && locationY === solLocationY && locationZ === solLocationZ){
locationName = "Sol";
console.log(shipName + " is at " + locationName)
alert("Arrived at " + locationName);
}
// Center of Galaxy
if(locationX===0 && locationY===0 && locationZ===0)
{
locationName = "Center of Galaxy";
console.log(shipName + " is at " + locationName)
alert("Arrived at " + locationName);
}
// SpacePort
if(locationX === spacePortLocationX && locationY === spacePortLocationY && locationZ === spacePortLocationZ)
{
locationName = "SpacePort"
console.log(shipName + " is at " + locationName)
alert("Arrived at " + locationName);
}
else{locationName = "empty space."}
}
// Purchase
function makePurchase()
{
if (locationName !== "SpacePort"){alert("You are not at the SpacePort.")}
if (locationName === "SpacePort")
{
var buy = prompt(userName + ", "+"what do you purchase?","Lasers, Sensors, or Repair Ship")
var purchase = buy.toUpperCase();{
if(purchase === "LASERS" && credits>=5000){lasers=lasers+10, credits= credits-5000, alert(userName+ "'s ship," + shipName +", has had it's lasers upgraded.")}
if(purchase === "SENSORS"){alert("These are not ready at this time.")}
if(purchase === "REPAIR SHIP"){shipDamage=0, alert(shipName+" has been repaired.")}
}
}
}
// Display Credits.
function creditsRemaining(){
console.log("Credits remaining: "+ credits);
}
// Display Laser Strength.
function laserStrength(){
console.log("Laser Strength: "+ lasers);
}
// Display Health.
function healthDisplay(){
console.log(health)
}
// Find things.
// Find current location.
function locationQuery(){
console.log(userName + " your ship, "+ shipName + ", is at " +locationName);
console.log("X = " +locationX);
console.log("Y = " +locationY);
console.log("Z = " +locationZ);
}
// Find Sol.
function solQuery(){
console.log("Sol is at:")
console.log("X = " + solLocationX);
console.log("Y = " + solLocationY);
console.log("Z = " + solLocationZ);
}
// Find SpacePort.
function spacePortQuery(){
console.log("The SpacePort is at:")
console.log("X = " + spacePortLocationX);
console.log("Y = " + spacePortLocationY);
console.log("Z = " + spacePortLocationZ);
}
// GoTo
function goToSpacePort(){
locationX = spacePortLocationX
locationY = spacePortLocationY
locationZ = spacePortLocationZ
locationName = "SpacePort"
}
// Quit
function quit()
{
var quitWindow = window.open("http://192.168.7.2:3000/workspace/myScripts/jordansSpaceExplorer/game/quitPage.html","_self");
}
//Loose
function loose()
{
var looseWindow = window.open("http://192.168.7.2:3000/workspace/myScripts/jordansSpaceExplorer/game/loose.html","_self");
}
// Suicide
function suicide()
{
health = health-100;
}
// Loose Conditions.
if(health <= 0){
console.log("You have lost the game.")
loose()
}
</script>
Controls: <br>
<button onclick="goEast()">Go East</button>
<button onclick="goWest()">Go West</button>
<br>
<button onclick="goNorth()">Go North</button>
<button onclick="goSouth()">Go South</button>
<br>
<button onclick="goUp()">Go Up</button>
<button onclick="goDown()">Go Down</button>
<br>
<button onclick="creditsRemaining()">Credits</button>
<button onclick="healthDisplay()">Heath</button>
<button onclick="laserStrength()">Lasers</button>
<br>
<button onclick="locationQuery()">Where am I?</button>
<button onclick="quit()">Quit</button>
<br>
SpacePort:
<br>
<button onclick="makePurchase()">Purchase</button>
<br>
Debug: <br>
<button onclick="spacePortQuery()">Where is SpacePort?</button><br>
<button onclick="solQuery()">Where is Sol?</button><br>
<button onclick="engageSensors()">Engage Sensors</button><br>
<button onclick="goToSpacePort()">SpacePort</button><br>
<button onclick="suicide()">Suicide!</button>
<button onclick="loose()">Loose</button>
</body>
</html>
Any help would be appreciated. I am new to programming so please take this into consideration.
You can changes your if statement to something along the lines of
function checkLost() {
// Loose Conditions.
if(health <= 0){
console.log("You have lost the game.")
loose()
}
setTimeout(checkLost, 100);
}
setTimeout(checkLost, 100);
Another way is to add the logic to the method that is lowering the life of the "ship" in this case. Like in you suicide method.
The if statement isn't in any function, so it will be executed when the script loads.
At that moment, the variable health has the value 100, so it is normal it should not call the function loose().
Maybe you should have a timer to check the health, or a function that you will call in every function that modifies the health.
You could do it like this:
function suicide()
{
health = health-100;
checkHealth();
}
function checkHealth() {
// Loose Conditions.
if(health <= 0){
console.log("You have lost the game.")
loose()
}
}
this is what i have but i need to record the problems that are wrong and have the user answer them until there right, keep prompting the user for the problem until its right this was a 4 part problem and part3 is to have the same problem repeat until its completed
ex.(7+1=6) repeat(7+1=4) repeat (7+1=8) next problem
the user has to complete 5 problems and they have to be correct
<html>
<script>
var file=0;
var fileIndex=new Array();
var user=prompt("what is "+a+" + "+b);
var arr=[user,user,user,user,user];
var a=Math.floor(Math.random()*11);
var b=Math.floor(Math.random()*11);
var answer=parseInt(a+b);
var user=prompt("what is "+a+" + "+b);
while (user != answer && file++ != 4) {
fileIndex.push(user);
user = prompt("what is " + a + "+" + b);
} else {
alert("answer is:" + answer);
}
}//close for
</script>
Update:
var a = 1,
b = 1;
var answer = a + b,
tick = 0;
var file_index = [], copy;
function randomize() {
a = Math.floor(Math.random() * 11);
b = Math.floor(Math.random() * 11);
answer = a + b;
}
while (1)
{
if ((copy = prompt("What is " + a + " + " + " b?", 0)) != answer)
{
if (!copy) break;
file_index.push(copy);
randomize();
} else if (tick == 4) {
break;
} else {
++tick;
}
}
jsFiddle Demo