Shniper
Shniper

Reputation: 854

Calling a function if an operation is not true

I have not seen anything that specifically answers this question. I intend to develop a text-based game that involves an action occurring every time an idle bar fills up(every x seconds passes) I'm just trying to figure out the very basic layout of my javascript that will make the battling system in this game.

I'm trying to make the computer re-calculate pDamThisRound if pDamThisRound is not > baseD which is this case is 10.

If it is greater than baseD my code works and prints pDamThisRound. But if it isn't the code just prints nothing.

var baseD = 10;
var pStr = 25;
var pDam = Math.floor((baseD + pStr) / 2);
var pDamThisRound;

var pDamTR  = function() {
    pDamThisRound = (Math.floor(Math.random() * 2 * pDam));
};

pDamTR();

if(pDamThisRound > baseD) {
document.getElementById("h").innerHTML = pDamThisRound;
}

else {
pDamTR();
}

https://jsfiddle.net/qfnvf1y8/4/

Upvotes: 0

Views: 66

Answers (5)

Ashwin Balamohan
Ashwin Balamohan

Reputation: 3332

You can use setTimeout to help you, with something like this:

var baseD = 10;
var pStr = 25;
var pDam = Math.floor((baseD + pStr) / 2);
var timeoutDuration = 3000; // 3 seconds


window.setTimeout(function () { 
  // execute the check and the update logic here

  var pDamThisRound = (Math.floor(Math.random() * 2 * pDam));

  if(pDamThisRound > baseD) {
    document.getElementById("h").innerHTML = pDamThisRound;
  }

}, timeoutDuration);

This will execute the check every 3 seconds

Upvotes: 0

Christoph
Christoph

Reputation: 51221

Several mistakes:

1) your else branch does not print anything
2) if you want pDamThisRound > baseD you need a while loop

while(pDamThisRound <= baseD) {
    pDamTR();
}

printpDamTR();

function printpDamTR(){
    document.getElementById("h").innerHTML = pDamThisRound;
}

The function printpDamTR would not be necessary, but it generally is a good idea to encapsulate such things, as you are probably going to use it more often. Also it increases the maintainability of your code since you loosen the dependencies between your JS and your HTML.

Upvotes: 0

Eugene Hauptmann
Eugene Hauptmann

Reputation: 1279

Here you go, a corrected version with a while operator:

https://jsfiddle.net/qfnvf1y8/6/

var baseD = 10;
var pStr = 25;
var pDam = Math.floor((baseD + pStr) / 2);
var pDamThisRound;

var pDamTR  = function() {
    pDamThisRound = (Math.floor(Math.random() * 2 * pDam));
};

pDamTR();

while(pDamThisRound <= baseD) {
    pDamTR();
}

if(pDamThisRound > baseD) {
document.getElementById("h").innerHTML = pDamThisRound;
}

else {
pDamTR();
}

Regards,

Eugene

Upvotes: 0

The function pDamTR is probably executing, the problem is that its assigning something to a variable, but doing nothing with it.

Upvotes: 1

David
David

Reputation: 218961

the code just prints nothing

That's because there isn't any code which prints anything. Look at your else block:

else {
    pDamTR();
}

What does that function do?:

var pDamTR  = function() {
    pDamThisRound = (Math.floor(Math.random() * 2 * pDam));
};

Nowhere do you produce any output.

Basically, if you want to print something then, well, print something. Just like you already do in your if block. For example:

document.getElementById("h").innerHTML = "something";

Upvotes: 2

Related Questions