2020-11-29

Understanding greater than equal to integer with decimals

main.js

// JavaScript source code
var quest = 0;
var gold = 0;
var maxrandomgold = 10;
var minrandomgold = 1;
var renown = 0;
var renownmultiplier = 0.2;

// For each click, add 1 quest number and random gold between 0-9.
function questclick(number) {
    quest = quest + number;
    if (renown >= (quest / 5)) {
        renownmultiplier = renownmultiplier * 2;
        maxrandomgold = Math.ceil((maxrandomgold + (renownmultiplier * 50))/1.5);
        minrandomgold = Math.ceil(maxrandomgold/2);
    }
    renown = Math.round((renown + renownmultiplier) * 100) / 100;
    gold = gold + (Math.floor(Math.random() * maxrandomgold) + minrandomgold);
    document.getElementById("quest").innerHTML = quest;
    document.getElementById("renown").innerHTML = renown;
    document.getElementById("gold").innerHTML = gold;
    document.getElementById("minrandomgold").innerHTML = minrandomgold;
    document.getElementById("maxrandomgold").innerHTML = maxrandomgold;
    document.getElementById("renownmultiplier").innerHTML = renownmultiplier;
}

index.html

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript" src="main.js"></script>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" type="text/css" href="interface.css" />
</head>
<body>
    <button onclick="questclick(1)">Quest!</button>
    <br />
    Completed Quests: <span id="quest">0</span>
    <br />
    Total Gold: <span id="gold">0</span>
    <br />
    Renown: <span id="renown">0</span>
    <br />
    Min Random Gold: <span id="minrandomgold">1</span>
    <br />
    Max Random Gold: <span id="maxrandomgold">10</span>
    <br />
    Renown Multiplier: <span id="renownmultiplier">0.2</span>
</body>
</html>

After pressing the Quest button 5 times, Renown is 1 and therefore greater than or equal to quest/5 (in this case, 1).

I expect the min random gold and max random gold to increase, as per the if statement.

However, it seems the if statement isn't triggered, and I can only put it down to Math.round() rounding to the nearest integer, and somehow the Greater Than Or Equal To if statement interpreting it incorrectly.

Or have I missed something completely obvious?



from Recent Questions - Stack Overflow https://ift.tt/39s7jMe
https://ift.tt/eA8V8J

No comments:

Post a Comment