0

I'm pretty new at Javascript (and web design). I'm trying to design an interactive webgame for purely educational purposes (I'm a professional educator. Most of my programming experience in creating self-contained Java apps).

In my HTML document, I have some internal script that creates a boolean array called hypotheses. I also have an external JS script called game.js that produces some visual output based on its own array, also called hypotheses, which I call using . I would like to update the external array from the internal one, but nothing I try works. The internal script is called from a form.

<script>
    var testedhypotheses = [];
    var hypotheses = hypArray(); //load up a boolean array of hypotheses.

function testHypothesis(theform, numTest){
    //update numbers
    theform.newhyp.value = theform.newhyp.value - numTest;

    //move hypotheses from one list to the other
    for(var i; i < numTest; i++){
        testedHypotheses.push(hypotheses.pop());
    }

    //pass the array of hypotheses to the external function. 
    updateHyp(testedHypotheses);
</script>

The function definitely works, and items are being moved from one array to another. However, the function updateHyp() is not being called. I've tried it a bunch of ways, but the most recent was to include this in the HTML file:

<script type="text/javascript"  src="game.js">
    var updateHyp = function (hypArray) {
        updateHypotheses(hypArray);

    };
</script>

Inside game.js, I have the following code:

var updateHypotheses = function(hypArray){
    var end = hypotheses.length;
    for(var i = end; i < hypArray.length; i++){
        var ind = hypotheses.length;
        var tf = hypArray[i];
        var rando = Math.random();
        var res rando < 0.5 ? 1 : 0; 
        hypotheses.push(new Hypothesis(i, tf, res));
        }       
    }
};

It seems that this code is never getting called. Any help would be much appreciated!!!

  • Since your calling it at global scope rather than in an event handler, ordering of scripts is rather important. Please clarify the order in which you placed these various script blocks. Also, what does the browser error console say? – Dark Falcon Sep 16 '14 at 17:22
  • Or perhaps your problem is: You cannot use both a `src` and include inline code within a single ` – Dark Falcon Sep 16 '14 at 17:23
  • @DarkFalcon http://stackoverflow.com/questions/1056325/javascript-inline-script-with-src-attribute – artm Sep 16 '14 at 17:29
  • Sorry for the delayed response. My problem turned out to be that I was using Processing without being aware of it. A hazard of learning "javascript" through Khan academy... I gave the processing/js file the id "geneclick" and then used the js `Processing.getInstanceById('geneclick').updateHypotheses(testedHypotheses);` That did the trick. – CapnDinosaur Nov 08 '14 at 00:01

0 Answers0