Instantly share code, notes, and snippets.

Embed
What would you like to do?
PinyinConverter
<!DOCTYPE html>
<html>
<head>
<title>Pinyin Converter</title>
<meta charset="UTF-8">
<meta name="author" content="Stefán Ólafsson">
<link rel="stylesheet" type="text/css" href="pinyinConvertStyle.css">
<script src="pinyinConvertCode.js"></script>
</head>
<body>
<div id="content">
<div>
<button id="umlaut" type="button" onclick="umlaut()">ü</button>
</div>
<div id="in">
<textarea id="input" rows="4" cols="25" onKeydown="Javascript: if (event.keyCode==13) convert(); else if (event.keyCode==46) erase();"></textarea>
</div>
<div id="buttons">
<table>
<tr><td><button id="change" type="button" onclick="convert()">Convert</button></td></tr>
<tr><td><button id="clear" type="button" onclick="erase()">Clear</button></td></tr>
</table>
</div>
<div id="out">
<textarea id="output" rows="4" cols="25"></textarea>
</div>
</div>
</body>
</html>
function convert() {
var withNumbersMixedCase = document.getElementById("input").value;
var withNumbers = withNumbersMixedCase.toLowerCase();
var withNumbersArray = withNumbers.split(" ");
var arrayLength = withNumbersArray.length;
var noNumbersArray = [];
var newChars1 = "āēīōūǖ".split('');
var newChars2 = "áéíóúǘ".split('');
var newChars3 = "ǎěǐǒǔǚ".split('');
var newChars4 = "àèìòùǜ".split('');
for (var i = 0; i < arrayLength; i++) {
if(withNumbersArray[i].indexOf("1") != -1){
var newstring = replace(withNumbersArray[i], newChars1);
noNumbersArray.push(newstring);
}
else if (withNumbersArray[i].indexOf("2") != -1) {
var newstring = replace(withNumbersArray[i], newChars2);
noNumbersArray.push(newstring);
}
else if (withNumbersArray[i].indexOf("3") != -1) {
var newstring = replace(withNumbersArray[i], newChars3);
noNumbersArray.push(newstring);
}
else if (withNumbersArray[i].indexOf("4") != -1) {
var newstring = replace(withNumbersArray[i], newChars4);
noNumbersArray.push(newstring);
}
else {
noNumbersArray.push(withNumbersArray[i]);
}
}
document.getElementById("output").value = noNumbersArray.join(" ");
}
function replace(string, array){
string = string.replace(/[1234]/, '');
var res;
if (string.indexOf("a") != -1) {
res = string.replace("a", array[0]);
}
else if (string.indexOf("e") != -1) {
res = string.replace("e", array[1]);
}
else if (string.indexOf("o") != -1) {
res = string.replace("o", array[3]);
}
else if (string.indexOf("u") != -1) {
if (string.indexOf("i") != -1) {
if (string.indexOf("u") < string.indexOf("i")) {
res = string.replace("i", array[2]);
}
else {
res = string.replace("u", array[4]);
}
}
else {
res = string.replace("u", array[4]);
}
}
else if (string.indexOf("i") != -1) {
res = string.replace("i", array[2]);
}
else if (string.indexOf("ü") != -1) {
res = string.replace("ü", array[5]);
}
else {
alert("Some word is missing a vowel");
}
return res;
}
function umlaut() {
document.getElementById("input").value += "ü";
}
function erase() {
document.getElementById("input").value = "";
document.getElementById("output").value = "";
}
#umlaut{
margin-left: 10px;
}
#input{
float: left;
margin: 10px;
}
#buttons{
float: left;
margin-top: 10px;
}
#change{
left: 20px;
width: 60px;
}
#clear{
float: left;
width: 60px;
}
#output{
float: left;
margin: 10px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment