Skip to content

Instantly share code, notes, and snippets.

@troyleach
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save troyleach/6ea3e0ea50c2ae5d57b1 to your computer and use it in GitHub Desktop.
Save troyleach/6ea3e0ea50c2ae5d57b1 to your computer and use it in GitHub Desktop.
Translate English to Swedish
// Represent a small bilingual lexicon as a Javascript object in the following fashion {"merry":"god", "christmas":"jul", "and":"och", "happy":gott", "new":"nytt", "year":"år"} and use it to translate your Christmas cards from English into Swedish.
var greetings = {
"merry": "god",
"christmas": "jul",
"and": "och",
"happy": "gott",
"new": "nytt",
"year": "ar"
};
var swedish = "";
function translate(phase) {
for (var name in greetings) {
if (greetings.hasOwnProperty(name)) {
var sentence = phase.split(/\s/);
for (var i = 0; i <= sentence.length; i++) {
if (sentence[i] === name) {
swedish += greetings[name] + " ";
}
}
}
}
swedish = swedish.trim();
swedish = swedish += "!";
return swedish[0].toUpperCase() + swedish.slice(1);
}
console.log(translate("merry christmas and happy new year"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment