0

Recently I've started learning JavaScript in use with HTML. I wanted to create a simple calculator for a game I am using. You select a type of ore, the amount of the ore left in the asteroid, the value mined each cycle, and the duration of one cycle, and the script is supposed to return the volume of ore to be mined and number of cycles required as well as their total duration.

I've tried several things, like enclosing the function in try statement, but that didn't work either. I am asking for your help trying to figure out what I did wrong and point me in the right way. Thanks in advance.

<html>
<head>
    <title>Mining Calculator - IndX: Industry Extension</title>
    <script type="text/javascript">     
        function oreCalc() {
        var ore = document.getElementById("ores").value;
        var volume;
        var amount = document.getElementById("amnt").value;
        var yield = document.getElementById("yield").value;
        var cycle = document.getElementById("cycle").value;
        try{
            switch(ore){
                case "veldspar":
                    volume = 0.1;
                    break;
                case "scordite":
                    volume = 0.15;
                    break;
                case "pyroxeres":
                    volume = 0.3;
                    break;
                case "plagioclase":
                    volume = 0.35;
                    break;
                case "omber":
                    volume = 0.6;
                    break;
                case "kernite":
                    volume = 1.2;
                    break;
                case "jaspet":
                    volume = 2;
                    break;
                case "hemorphite":
                    volume = 3;
                    break;
                case "gneiss":
                    volume = 5;
                    break;
                case "dark_ochre":
                    volume = 8;
                    break;
                case "spodumain":
                    volume = 16;
                    break;
                case "crokite":
                    volume = 16;
                    break;
                case "arkonor":
                    volume = 16;
                    break;
                case "mercoxit":
                    volume = 40;
                    break;
            }
            var rockvolume = amount * volume;
            var cycles = rockvolume / yield;
            var seconds = cycles * cycle;
            var minutes;
            while(seconds > 60)
            {
                seconds -= 60;
                minutes++;
            }
            var text = "The asteroid has " + rockvolume + " m3 of ore. \n You'll mine it in " + Math.ceil(cycles) +
             "  cycles. \n It will take you " + minutes + "min " +   Math.ceil(seconds) "s to mine.";
            document.getElementById("output").innerHTML = text;
            }
            catch(err){
                var text = "Input all required fields";
                document.getElementById("output").innerHTML = text;
            }
        }
    </script>
</head>
<body>
    <table><tr><td>Ore:</td><td>
        <select name="ores" id="ores" onchange="oreCalc()"> 
            <option value="veldspar">Veldspar</option>
            <option value="scordite">Scordite</option>
            <option value="pyroxeres">Pyroxeres</option>
            <option value="plagioclase">Plagioclase</option>
            <option value="omber">Omber</option>
            <option value="kernite">Kernite</option>
            <option value="jaspet">Jaspet</option>
            <option value="hemorphite">Hemorphite</option>
            <option value="gneiss">Gneiss</option>
            <option value="dark_ochre">Dark Ochre</option>
            <option value="spodumain">Spodumain</option>
            <option value="crokite">Crokite</option>
            <option value="arkonor">Arkonor</option>
            <option value="mercoxit">Mercoxit</option>
        </select></td><td rowspan="4"><p id="output"></p></td></tr>
        <tr><td>Amount:</td><td> 
            <input type="number" name="amount" id="amnt" oninput="oreCalc()">
        </td></tr>
        <tr><td>Yield:</td><td>
            <input type="number" name="yield" id="yield" oninput="oreCalc()">
        </td></tr>
        <tr><td>Cycle duration:</td><td><input type="number" name="cycle" id="cycle" oninput="oreCalc()"></td></tr>
    </table>
</body>
</html>
  • 2
    It would be really helpful if you created a jsFiddle and copied your code over there so we can debug it. – gwin003 Feb 02 '15 at 20:07
  • 1
    You have a syntax error in the JavaScript. `Math.ceil(seconds) "s to mine."` should be `Math.ceil(seconds) + "s to mine."` –  Feb 02 '15 at 20:14
  • @Gwin003 http://jsfiddle.net/#&togetherjs=6B8FkPihMg – Dominik Spilka Feb 02 '15 at 20:15
  • @JustinY Thank you for pointing out, I've tried correcting it, but it didn't fix the problem yet. – Dominik Spilka Feb 02 '15 at 20:16
  • Fixed it for me. http://jsfiddle.net/k8jvv4b7/ The issue now is the interaction between HTML and JS in jsFiddle. http://stackoverflow.com/questions/5431351/simple-example-doesnt-work-on-jsfiddle –  Feb 02 '15 at 20:20
  • Your HTML events are unable to find your javascript function. Check the browser console to see the errors. – gwin003 Feb 02 '15 at 20:20
  • Your fiddle code outputs fine for me, but you need to change `function oreCalc() {` to `window.oreCalc = function() {` so that the inline event handlers in the HTML pane can see the function defined in the JavaScript pane. That could not happen in the complete example you've given in your question, however, because your `oreCalc` in the in-question example is already top-level. – apsillers Feb 02 '15 at 20:20
  • I tried JustinYs solution out of jsFiddle and it really fixed the problem. Thanks again, such a newbie mistake did I make. – Dominik Spilka Feb 02 '15 at 20:26

1 Answers1

0

To track down these sorts of problems you need to use your browser's error console.

  • In Firefox it's Menu -> Developer -> Web Console
  • In Chrome it's Menu -> More Tools -> Javascript Console
  • In IE I think you hit F12 a click console.

I'm using Firefox and I see the following error on your page:

SyntaxError: missing ; before statement     ore.html:66

And if you go to around line 66 you'll see:

var text = "The asteroid has " + rockvolume + " m3 of ore. \n You'll mine it in " + Math.ceil(cycles) +
             "  cycles. \n It will take you " + minutes + "min " +   Math.ceil(seconds) "s to mine.";

Note the missing "+" after Math.ceil(seconds) but before "s to mine."? Add the plus sign and it should now work.

Btw, it's often better to use an external file for Javascript. I find it makes it much easier to manage and track down issues.

ChrisD
  • 3,378
  • 3
  • 35
  • 40