Created
April 1, 2015 22:03
-
-
Save marekjalovec/0e4b71b5f6d6b3af1f23 to your computer and use it in GitHub Desktop.
Sapho puzzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head lang="en"> | |
<meta charset="UTF-8"> | |
<title></title> | |
</head> | |
<body> | |
<script> | |
/** | |
* Convert text to pig-latin | |
* | |
* @param str string | |
* @returns string | |
*/ | |
function pigMe(str) { | |
// split by spaces | |
str = str.split(' '); | |
// process the words | |
for (var i in str) { | |
// protection | |
if (!str.hasOwnProperty(i)) continue; | |
var word = str[ i ]; | |
// if word contains hyphen, process "recursively" | |
if (word.indexOf('-') !== -1) { | |
str[ i ] = pigMe(word.replace('-', ' ')).replace(' ', '-'); | |
continue; | |
} | |
// ignore word ending with 'way' | |
if (word.substr(-3) === 'way') { | |
continue; | |
} | |
// map uppercase letters | |
var upperMap = []; | |
for (var j in word) { | |
if (!word.hasOwnProperty(j)) continue; | |
if (word[ j ] === word[ j ].toUpperCase() && word[ j ].match(/[a-z]/i)) { | |
upperMap.push(j); | |
} | |
} | |
// convert word to lowercase | |
word = word.toLowerCase(); | |
// map punctuation | |
var punctMap = {}, | |
punctuations = /[',\.]/g; | |
for (j in word) { | |
if (!word.hasOwnProperty(j)) continue; | |
// save punctuation and its position from the end of the world | |
if (word[ j ].search(punctuations) !== -1) { | |
punctMap[ word.length - j - 1 ] = word[ j ]; | |
} | |
} | |
// remove punctuations | |
word = word.replace(punctuations, ''); | |
// word begins with consonant -- move first letter to the end and ad 'ay' | |
var consonants = 'bcdfghjklmnpqrstvxzw'; // http://en.wikipedia.org/wiki/Consonant | |
var vowels = 'aeiouy'; // http://en.wikipedia.org/wiki/Vowel | |
if (consonants.indexOf(word[ 0 ]) !== -1) { | |
word = word.substr(1) + word.substr(0, 1) + 'ay'; | |
} | |
// word begins with a vowel -- append 'way' | |
else if (vowels.indexOf(word[ 0 ]) !== -1) { | |
word += 'way'; | |
} | |
// convert letters to uppercase | |
for (j in upperMap) { | |
if (!upperMap.hasOwnProperty(j)) continue; | |
var index = parseInt(upperMap[ j ]); | |
word = | |
word.substr(0, index) | |
+ word.substr(index, 1).toUpperCase() | |
+ word.substr(index + 1); | |
} | |
// put punctuation back | |
for (index in punctMap) { | |
if (!punctMap.hasOwnProperty(index)) continue; | |
word = | |
word.substr(0, word.length - index) | |
+ punctMap[ index ] | |
+ word.substr(word.length - index); | |
} | |
str[ i ] = word; | |
} | |
return str.join(' '); | |
} | |
var str = "Hello apple stairway can't end. this-thing Beach McCloud"; | |
document.write(str); | |
document.write('<br/>'); | |
document.write(pigMe(str)) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output
Hello apple stairway can't end. this-thing Beach McCloud
Ellohay appleway stairway antca'y endway. histay-hingtay Eachbay CcLoudmay