简体   繁体   中英

How Do You Make Your Entire OWN Language Coder? I Tried This.. (doesn't work)

I tried to make it all connect but nothing is working.. I am horrible at JavaScript.. It says that it is 'undefined'. I think it is defined..

var convert = function (x) {
    alert(dxc(x));

    function dxc(m) {
        stg(m.charAt(0));
    }

    function stg(d) {
        if (d === "d") {
            d = "p";
        }
    }
};

var conversion = prompt("What do you want to translate?");
convert(conversion);

Edit: This is just for the idea of the entire thing, I was no where near done..

You forgot to return the values you want to return

var convert = function(x) {
    alert(dxc(x));

    function dxc(m) {
        return stg(m.charAt(0));          
    }
    function stg(d) {
        if (d === "d") {
            d = "p";
        }
        return d;
    }
};

var conversion = prompt("What do you want to translate?");
convert(conversion);

Your functions aren't returning anything.

Try:

var convert = function (x) {
    alert(dxc(x));

    function dxc(m) {
        return stg(m.charAt(0));
    }

    function stg(d) {
        if (d === "d") {
            d = "p";
        }

        return d;
    }
};

d is just a name that points to a value; when you do d = "p" , you're changing what d points to, but you changed only d ; the source of d (in particular, m.charAt(0) ) is left unchanged.

You'll have to return the modified string manually:

function dxc(m) {
    return stg(m.charAt(0)) + m.substring(1);
}

function stg(d) {
    if (d === "d") {
        return "p";
    }else{
        return d;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM