var spanish = ["arbol", "casa", "el", "la", "quintas", "es", "verde", "y", "roja"];
var english = ["tree", "house", "the", "the", "crack", "is", "green", "and", "red"];
var traductor = function () {
if (spanish.length === english.length) {
var spanishText = prompt("Introduce tu frase:"); // por ejemplo la casa de quintas es verde ;)
// esta función la he buscado, que no la conocía.
var spanishTextSplit = spanishText.split(" ");
var englishTextSplit = [];
// se rastrea palabra por palabra
for (i = 0; i<spanishTextSplit.length; i++) {
var j=0; //recorre el diccionario
var k=0; //escapa del while cuando cambia a 1
while (j<spanish.length) {
if(spanishTextSplit[i] === spanish[j]) {
englishTextSplit[i] = english[j];
k=1;
}
j++;
}
// por si no encuentra algun palabro.
if (k === 0) {
englishTextSplit[i] = "¿" + spanishTextSplit[i] + "?";
}
console.log (englishTextSplit[i] + " ");
}
}
else
{
console.log ("Wops! The dictionary has an error: there are " + spanish.length + " spanish words and " + english.length + " english ones") ;
}
};
traductor();
Refactorings
No refactoring yet !
claimid.com/ompemi
January 25, 2012, January 25, 2012 11:44, permalink
kj
var spanish = ["arbol", "casa", "el", "la", "quintas", "es", "verde", "y", "roja"];
var english = ["tree", "house", "the", "the", "crack", "is", "green", "and", "red"];
var traductor = function () {
if (spanish.length === english.length) {
var spanishText = prompt("Introduce tu frase:"); // por ejemplo la casa de quintas es verde ;)
// esta función la he buscado, que no la conocía.
var spanishTextSplit = spanishText.split(" ");
var englishTextSplit = [];
// se rastrea palabra por palabra
for (i = 0; i<spanishTextSplit.length; i++) {
var j=0; //recorre el diccionario
var k=0; //escapa del while cuando cambia a 1
while (j<spanish.length) {
if(spanishTextSplit[i] === spanish[j]) {
englishTextSplit[i] = english[j];
k=1;
}
j++;
}
// por si no encuentra algun palabro.
console.log (englishTextSplit[i] + " ");
}
}
else
{
console.log ("Wops! The dictionary has an error: there are " + spanish.length + " spanish words and " + english.length + " english ones") ;
}
};
traductor();
claimid.com/ompemi
January 25, 2012, January 25, 2012 11:44, permalink
kj
var spanish = ["arbol", "casa", "el", "la", "quintas", "es", "verde", "y", "roja"];
var english = ["tree", "house", "the", "the", "crack", "is", "green", "and", "red"];
var traductor = function () {
if (spanish.length === english.length) {
var spanishText = prompt("Introduce tu frase:"); // por ejemplo la casa de quintas es verde ;)
// esta función la he buscado, que no la conocía.
var spanishTextSplit = spanishText.split(" ");
var englishTextSplit = [];
// se rastrea palabra por palabra
for (i = 0; i<spanishTextSplit.length; i++) {
var j=0; //recorre el diccionario
var k=0; //escapa del while cuando cambia a 1
while (j<spanish.length) {
if(spanishTextSplit[i] === spanish[j]) {
englishTextSplit[i] = english[j];
k=1;
}
j++;
}
// por si no encuentra algun palabro.
console.log (englishTextSplit[i] + " ");
}
}
else
{
console.log ("Wops! The dictionary has an error: there are " + spanish.length + " spanish words and " + english.length + " english ones") ;
}
};
traductor();
claimid.com/ompemi
January 25, 2012, January 25, 2012 11:44, permalink
kj
var spanish = ["arbol", "casa", "el", "la", "quintas", "es", "verde", "y", "roja"];
var english = ["tree", "house", "the", "the", "crack", "is", "green", "and", "red"];
var traductor = function () {
if (spanish.length === english.length) {
var spanishText = prompt("Introduce tu frase:"); // por ejemplo la casa de quintas es verde ;)
// esta función la he buscado, que no la conocía.
var spanishTextSplit = spanishText.split(" ");
var englishTextSplit = [];
// se rastrea palabra por palabra
for (i = 0; i<spanishTextSplit.length; i++) {
var j=0; //recorre el diccionario
var k=0; //escapa del while cuando cambia a 1
while (j<spanish.length) {
if(spanishTextSplit[i] === spanish[j]) {
englishTextSplit[i] = english[j];
k=1;
}
j++;
}
// por si no encuentra algun palabro.
console.log (englishTextSplit[i] + " ");
}
}
else
{
console.log ("Wops! The dictionary has an error: there are " + spanish.length + " spanish words and " + english.length + " english ones") ;
}
};
traductor();
claimid.com/ompemi
January 25, 2012, January 25, 2012 11:44, permalink
kj
var spanish = ["arbol", "casa", "el", "la", "quintas", "es", "verde", "y", "roja"];
var english = ["tree", "house", "the", "the", "crack", "is", "green", "and", "red"];
var traductor = function () {
if (spanish.length === english.length) {
var spanishText = prompt("Introduce tu frase:"); // por ejemplo la casa de quintas es verde ;)
// esta función la he buscado, que no la conocía.
var spanishTextSplit = spanishText.split(" ");
var englishTextSplit = [];
// se rastrea palabra por palabra
for (i = 0; i<spanishTextSplit.length; i++) {
var j=0; //recorre el diccionario
var k=0; //escapa del while cuando cambia a 1
while (j<spanish.length) {
if(spanishTextSplit[i] === spanish[j]) {
englishTextSplit[i] = english[j];
k=1;
}
j++;
}
// por si no encuentra algun palabro.
console.log (englishTextSplit[i] + " ");
}
}
else
{
console.log ("Wops! The dictionary has an error: there are " + spanish.length + " spanish words and " + english.length + " english ones") ;
}
};
traductor();
Ants
January 25, 2012, January 25, 2012 22:07, permalink
- Changed data structures to simulate an associative array in JavaScript.
- Separated concerns: translateWords() just translates or remaps the words and traductor() takes care of input and output management.
- Made translateWords() take the dictionary to be used as a parameter so that the function can be reused.
There is still room for more improvement in the code below. For example, I'm not happy with the way undefined words are always quoted the Spanish way. What happens when it's English to Spanish?
var spanishToEnglish = {
"arbol" : "tree",
"casa" : "house",
"el" : "the",
"la" : "the",
"quintas" : "crack",
"es" : "is",
"verde" : "green",
"y" : "and",
"roja" : "red"
};
function translateWords(dict, words)
{
var values = [];
for(i = 0; i < words.length; i++)
{
var value = dict[words[i]];
if (value == undefined)
value = "¿" + words[i] + "?";
values.push(value);
}
return values;
}
function traductor()
{
var phrase = prompt("Introduce tu frase:");
var output = translateWords(spanishToEnglish, phrase.split(" "));
for(i = 0; i < output.length; i++)
console.log(output[i]);
}
traductor();
that's it