hajimal
hajimal

Reputation: 161

Where to return result of recursive function

I know this is a stupid problem, but I can't solve it for hours.

I have recursive function which takes JSON object and creates XML string:

function JSONtoXML(object, xml) {
    var name = '';
    for (var prop in object) {
        if (object.hasOwnProperty(prop)) {
            if (typeof object[prop] === 'object'){
                JSONtoXML(object[prop], xml);
            }
            else {
                if (prop === 'name') {
                    name = object[prop];
                    xml += '<' + name;
                }
                else if (prop === 'version') {
                    xml += ' version="' + object[prop] + '">\n<params>\n';
                }
                else if (prop === 'value') {
                    xml += '>' + object[prop] + '</' + name + '>';
                }
                else {
                    xml += ' ' + prop + '="' + object[prop] + '"';
                }
            }
        }
    }
};

I would like if I could just return the XML string when the function finishes, but I have no clue where to write the return xml; part. Anywhere I put it, it returns only part of the string.

Upvotes: 1

Views: 65

Answers (2)

aleha_84
aleha_84

Reputation: 8541

remove xml as parametr

function JSONtoXML(object) {
    var name = '';
    var xml = '';
    for (var prop in object) {
        if (object.hasOwnProperty(prop)) {
            if (typeof object[prop] === 'object'){
                xml += JSONtoXML(object[prop]);
            }
            else {
                if (prop === 'name') {
                    name = object[prop];
                    xml += '<' + name;
                }
                else if (prop === 'version') {
                    xml += ' version="' + object[prop] + '">\n<params>\n';
                }
                else if (prop === 'value') {
                    xml += '>' + object[prop] + '</' + name + '>';
                }
                else {
                    xml += ' ' + prop + '="' + object[prop] + '"';
                }
            }
        }
    }

    return xml;
};

Upvotes: 3

Naeem Shaikh
Naeem Shaikh

Reputation: 15725

function JSONtoXML(object, xml) {
    var name = '';
    for (var prop in object) {
        if (object.hasOwnProperty(prop)) {
            if (typeof object[prop] === 'object'){
                JSONtoXML(object[prop], xml);
            }
            else {
                if (prop === 'name') {
                    name = object[prop];
                    xml += '<' + name;
                }
                else if (prop === 'version') {
                    xml += ' version="' + object[prop] + '">\n<params>\n';
                }
                else if (prop === 'value') {
                    xml += '>' + object[prop] + '</' + name + '>';
                }
                else {
                    xml += ' ' + prop + '="' + object[prop] + '"';
                }
            }
        }
    }

   return xml;// Here return the result
};

Return when you have done processig! that is before function closure in this case.

Upvotes: 2

Related Questions