/    Sign up×
Community /Pin to ProfileBookmark

Switching letters

I have an input and output text area and a convert button. It’s programmed so that, if I type for example the letter “k” in the input text area, I get back the letter w, which is exactly what I want. But I would like to use it for 2 letters as well. For example I want if I type “kh” I want to get “p”. Would that be possible? especially if it’s in a simple code like the one below:

[CODE]function convertchar(letter)
{
if (letter==”k”) {return “w”}
else if (letter==”g”) {return “z”}
else if (letter==”t”) {return “d”}[/CODE]

Something simple as this (even though it is wrong:

[QUOTE]

else if (letter==”k+h”) {return “p”}

[/QUOTE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@tirnaAug 12.2010 — I would do domething like this.

1) The convert button sends the current value of the textarea to your function on onchange or onkeyup, depending on what you actually want to do.

2) Then compare the sent text string to predefined values and return values accordingly.

[CODE]
function convertchar(str) {
if (str=="k") {return "w"}
else if (str=="g") {return "z"}
else if (str=="t") {return "d"}
else if (str=="kh") {return "p"}
}
[/CODE]
Copy linkTweet thisAlerts:
@Declan1991Aug 12.2010 — Your function name is convertchar, which implies you are just sending one letter to it, so we have to see the full script.

If you take the full string from the text area, preform all two letter substitutions on it using regular expressions (so that if you have kak, you will get waw, not wak), and then all single letter substitutions on it, that will work. Something like this.<i>
</i>var str = /* From text area */;
str = str.replace(/kh/gi,"p");
str = str.replace(/k/gi,"w");
str = str.replace(/g/gi,"z");
Obviously, if you are doing this on key-up, you will have to prevent the function running if the last letter typed is a possible digraph.
×

Success!

Help @skylab spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...