RadleyMith
RadleyMith

Reputation: 1343

Function not returning correct value

so I have these two functions and then another function calling them. It seems like I'm losing scope, between function calls.

var determineInteractionType = function( interaction ){

    var param = interaction.parameterSet.param;

    param.forEach(function( parameter, index, array ){
        if( parameter.name === "INTERACTION-TYPE" ){
            return parameter.value;
        }
    });
    return null;
};

var getInteraction = function( id ){        

    customInteractions.forEach( function(interaction, index, array){            
        if( interaction.id == id ){
            alert( id );
            return interaction;
        }
    });
    return null;
};

Here is the piece of code calling the functions. The error is that even though getInteraction is returning a value it is showing up that determineInteraction is getting passed an argument of null.

var _convertStemFromEAS = function(stem) {
    var reg = new RegExp('@\{PRESENTATION-HTML-INTERACTION\}="(.*?)"');
    var result;
    var count = 1;      

    while ((result = reg.exec(stem)) !== null) {

        var Match = result[0];
        var dropdownGuid = result[1];

//The Error seems to be right here
        var interactionType = determineInteractionType( getInteraction( id ) );


        if( interactionType === "shortTextInteraction" ){           
            var escaped = $('<div/>').text('<select id="' + NewTmpGuid() + '" data-choice-id="' + dropdownGuid + '" style="width:100px;" data-count="' + count + '" class="easSelection"><option>DD' + count + '</option></select>').html();
        }else if( interactionType === "essayTextInteraction" ){

        }

        count += 1;
        stem = stem.replace(Match, escaped);
    }   
    return stem;    
};

Upvotes: 2

Views: 349

Answers (1)

Adam Jenkins
Adam Jenkins

Reputation: 55663

determineInteraction is returning null because that's what it's coded to return.

When you return from the inner function in the forEach loop, you're not returning from determineInteraction.

What you want to do is something like this:

var determineInteractionType = function( interaction ){

    var 
    returnValue = null,
    param = interaction.parameterSet.param;


    param.forEach(function( parameter, index, array ){
        if( parameter.name === "INTERACTION-TYPE" ){
            returnValue = parameter.value;
        }
    });

    return returnValue;
};

Upvotes: 1

Related Questions