0

I'm trying to create a script that represents an elementary grade multiplication aid. The script chooses two random numbers and multiplies them together. You then enter your answer and if it is correct it states good job, if you are incorrect it will state so and return you to the prompt (it won't let you continue until you get it right). Afterwards it will ask if you'd like to continue yes or no. If yes, Do loop back up to initial function to return another set of random numbers to multiply, wash, rinse, repeat.

For some reason my functions aren't working at all. You will see in the code I tried placing window.alerts just so I know its running through that particular function. Alas, no alert in either function. I know that all the code isn't here for what I'm trying to accomplish, I'm not necessarily looking for someone to write out the whole script (need to learn it myself) but functions will not work. Syntax? Any help is appreciated!

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
    document.writeln ("<h2 style = 'color:blue' >");
    document.writeln ("Hello! Are you ready to learn some multiplication?");

    /* I don't believe either function is working properly and 
       I'm not entirely sure its syntax. I'm doin something wrong and 
       I'm gouging my eyes out!!! */
    do {    
        function start() {
            var button = document.getElementById("go");
            button.addEventListener("click", fetchnumbers, false);
            /* I add this to pop up during function 1 Just to check to see if its working*/
            window.alert ("function 1");
        }

        function fetchnumbers() {
            var math1 = Math.floor ((Math.random()*12)+1);
            var math2 = Math.floor ((Math.random()*12)+1);
            // I add this to pop up and display the numbers from the variables to ensure correct syntax for math function but heck no popup
            window.alert (math1 + math2 );
        }

        finish = window.prompt ("Are we done for today?\n\nYes = 1\nNo = 2");   
    } while (finish == 2);  
    </script>
</head>
<body>
    <br />
    <br />
    <form>
        <input id="go" type="button" value="Yeah!">
        <!--the id is referenced in the start function.-->
    </form>
</body>
</html>
Qantas 94 Heavy
  • 14,790
  • 31
  • 61
  • 78
  • 3
    @Niloct: [It exists.](http://es5.github.io/#x12.6.1) – icktoofay Apr 13 '14 at 02:51
  • Where are you calling `start`? Also, function definitions should *not* be in blocks. – Qantas 94 Heavy Apr 13 '14 at 02:51
  • To my knowledge I call the first function on "click" of the form button. And the second function is called from the first function. I'm not 100% sure of the blocks you are referring to do you mean {}? – PimPedOutGeese Apr 13 '14 at 02:53
  • I see the edits, thank you guys for taking a look. Feel like such a noob posting here :( – PimPedOutGeese Apr 13 '14 at 02:59
  • @user3528119: we haven't changed anything, just the formatting to make it more clear. :) I don't see the `start` function called anywhere though -- from this HTML alone it won't be called by clicking the button with ID `go`. – Qantas 94 Heavy Apr 13 '14 at 03:00
  • @icktoofay The joy of humbleness =) – Niloct Apr 13 '14 at 03:01
  • So that's the missing link. The function doesn't just start on its own. I thought javascript just kinda read everything cascading down. Would you be so kind to show an example of how to call a function in this case? – PimPedOutGeese Apr 13 '14 at 03:04
  • http://jsfiddle.net/gy8k8/ it's a bit weird since in your code you have to stop the `while` prompt to then finally being able to click the `button` – Niloct Apr 13 '14 at 03:08
  • I haven't quite worked that out. Since its script its gonna run that first. I think I know whats missing! I need to run the script as the page loads... something like: `window.addEventListener("load", start, false);` – PimPedOutGeese Apr 13 '14 at 03:10
  • Don't use function declaration syntax to define functions in a block. Define them outside the `do-while` loop, or define them as `var start = function() {...` – cookie monster Apr 13 '14 at 03:15
  • 1
    [Learn how to](http://www.creativebloq.com/javascript/javascript-debugging-beginners-3122820) [**debug** JavaScript](https://developers.google.com/chrome-developer-tools/docs/javascript-debugging). Set breakpoints, inspect variables. Debugging 101. – Felix Kling Apr 13 '14 at 03:17
  • @cookiemonster - Thanks I see what you mean. functions will be removed from `do-while`. @Felix Kling interesting I will be taking a look. Taking a college course and, again, this is new to me so def a learning curve. Mistakes will be made – PimPedOutGeese Apr 13 '14 at 03:22

1 Answers1

0

Your methods are... a bit weird... but the main issue is that your start function is never called.

You should move your functions out of the do loop scope as I imagine you are intending on calling start() one time when your page loads. Currently your functions can only be called inside the do loop scope.

function start() {
    var button = document.getElementById("go");
    button.addEventListener("click", fetchnumbers, false);
    /* I add this to pop up during function 1 Just to check to see if its working*/
    window.alert ("function 1");
}

function fetchnumbers() {
    var math1 = Math.floor ((Math.random()*12)+1);
    var math2 = Math.floor ((Math.random()*12)+1);
    // I add this to pop up and display the numbers from the variables to ensure correct syntax for math function but heck no popup
    window.alert (math1 + math2 );
}
do { 
    finish = window.prompt ("Are we done for today?\n\nYes = 1\nNo = 2");   
} while (finish == 2);  

Also, I recommend against document.write functions. They are more trouble then they are worth.

Example here http://jsfiddle.net/W2WY7/

Biff MaGriff
  • 7,901
  • 9
  • 60
  • 95
  • I see.. I'm sorry I'm absolutely new to javascript. The way I write it out is always weird at first then I clean it up so yea. i always put fluff in just to check the code. Ok yea I added `window.addEventListener("load", start, false);` so that calls the function. I'm going to take the `do` statement out and see where it goes. Thanks for your help! – PimPedOutGeese Apr 13 '14 at 03:19
  • @PimPedOutGeese Don't worry about being new. We were all new once and javascript is not exactly the easiest language to work with. Good luck with your course. – Biff MaGriff Apr 13 '14 at 03:25