How can I make a simple mind game, that includes addition, multiplications

I dont know more about programming but after reading this thread I am willing to learn. It really interesting and mind massager.

with this code I can count backwards but I don’t know where to place my alert to stop the loop.

<html>
<head>

<script type=“text/javascript”>
function startTime()
{
var today=new Date();

var s=today.getSeconds();
// add a zero in front of numbers>10

s=checkTime(s);
document.getElementById(‘txt’).innerHTML=s;
t=setTimeout(‘startTime()’,500);
}

function checkTime(i)
{
if (i<1000)
{
i=“60” - i;
}
return i;

}
{
alert(“Game over!”);
}
</script>

</head>
<body onload=“startTime()”>
<div id=“txt”></div>

</body>
</html>

At some point in the code i need to put

alert(“Game over!”);

could you explain where is the correct place to break the loop, please?

Since you are using setInterval on the startTime function, it would be in there where you do that.

Thank you - the code is now working.

I need to initiate two javascripts, at the same time, that is when the player clicks on the answer button.

at the moment using the following codes, I can inititate the two scripts, at run time.

<input name=“submit” type=“submit” class=“floatingAnswer” onclick=“timeMsg()onfocus=“doTimer()” value=“answer” >


but want to know if there is a better way to join the two events using the onclick action, please?

for example, when trying the following code - the doTime stops working.

<input name=“submit” type=“submit” class=“floatingAnswer” onclick=“timeMsg()” onclick=“doTimer()” value=“answer” >

A better way to do that is as a part of the onsubmit event.

For example, before:


form.onsubmit = this.ask;

And after:


form.onsubmit = function () {
    timeMsg();
    doTimer();
    ask();
};

I am not able to make the code to work - could you indicate where I should place this code please?
for example, should it be in the head section of the html document or in the script.

The example that I gave is from part of the init code.

i am still not able to make the code to work - i have tried adding the code in different places of the javascrip, but i tent to either get error or it just doesn’t response at run time. for example,

window.questions = {
history: ,

init: function (form, config) {
var prop;
this.minValue = 1;
this.maxValue = 20;
this.totalQuestions = 10;
this.startTime = new Date();
form.questions = this;
form.onsubmit = this.ask;



form.questions.show(form);

for (prop in config) {
    if (config.hasOwnProperty(prop)) {
        this[prop] = config[prop];
form.onsubmit = function () {
timeMsg();
doTimer();
ask();

};
}
}
},

could you explain how this code should be used please?

Replace the first piece of code from post #45 with the second piece of code.

THANK YOU!

could you advise how I can place the content of the alert, show below, into a text or label box on my html page i.e. the form, please?

alert(question.guess + ’ is not the right answer.

’ + question.first + ’ ’ + question.operator + ’ ’ + question.second + ’ is ’ + question.answer);

…for example, is there a way that I could write

form.elements.CorrectAnswer.text = question.guess + ’ is not the right answer.

’ + question.first + ’ ’ + question.operator + ’ ’ + question.second + ’ is ’ + question.answer

It’s not possible to have multiple lines within a text or label box.

You can though use a div element to contain some content, for example:


<div class="correctAnswer"></div>


var correctAnswer = form.querySelector('.correctAnswer');
correctAnswer.innerHTML = '<p>' + question.guess + ' is not the right answer.</p>' +
    '<p>' + question.first + ' ' + question.operator + ' ' + question.second + ' is ' + question.answer + '</p>';

THANK YOU!

As a general question, is it also possible to display the content of a variable such as the var ‘correctAnswer’ outside of a form opening <form> and closing </form> tag, in HTML document?

for example, can we display the content of a variable in a div tag, that does not belong to a form tag.

As a second question, I have often seen experienced programmers, like yourself, using the <span> tag. however, I don’t fully understand the correct use of the tag. hence, I would greatly appreciate it if you could comment on use of the <span> tag, please.

Yes we can, with document.getElementById(‘identifier’)

A span tag is an inline version of the div tag, and that’s about the only difference between the two. Neither of them have a context that applies to them, as for example occurs with other tags such as the p tag, or the ul tag.

When it’s not suitable to edit the entire contents of an element, as we have been doing with the div tag, that’s where you can use a span tag instead to provide an inline section to be edited instead.

Thank you for your detailed and clear explanation.

for this game, I would like to allow players to change question numbers at run time. for example, by using a drop down list for players to select or a text box for players to type in their number of questions.

could you please advise me, how I should approach this code please?

for this game, I would like to allow players to change question numbers at run time. for example, by using a drop down list for players to select or a text box for players to type in their number of questions.

could you please advise me, how I should approach this code please?

Do you see in the code how there is a value for the number of questions?

Do you see in the code how there is a value for the number of questions?

this.totalQuestions = 10;

yes, using the following code I can changed the number of questions.

this.totalQuestions = 30;

but i dont know how to change this at run time - upon player selection/request?