0

I am making a high school level Spanish 1 & 2 translator but I have run into a bug; After you are prompted for the word, instead of just giving you the translation it prints out the entire list of translations.

I am using my basic java knowledge gained from the site Codeacademy.com and can not figure out what causes this bug.

P.S. If you can help me in making this process with the if statements easier and quicker I would greatly appreciate it.

Code:

var word = prompt("Word to translate(lower case only):");

//English
var hi;
var white;

//Translate
var hola = hi;
var blanco = white;

var translate = function(word)
{
    return word;
};

//Spanish to English
if(translate() === hola)
{
    console.log("hi");
}

if(translate() === blanco)
{
    console.log("white");
}

//English to Spanish
if(translate() === hi)
{
    console.log("hola");
}

if(translate() === white)
{
    console.log("blanco");
}
Michal
  • 2,532
  • 1
  • 19
  • 27
Zeldacorp
  • 3
  • 1
  • This isn't Java, does kind of look like Javascript though. Perhaps you have confused the 2. – Josh M Sep 07 '13 at 00:43
  • I think he means javascript... And it seems the translate function doesn't actually do anything? – Ryan Hurling Sep 07 '13 at 00:45
  • 1
    First you need to know what's difference between Java and JavaScript http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java – fujy Sep 07 '13 at 00:45
  • Like I said basic knowledge; I did mean javascript, I did forget to go back and check the diference. – Zeldacorp Sep 07 '13 at 01:05
  • Also how it was working before I added more words was the if(translate() === hola) part just takes whatever 'word' is(in this case hola) and checks if it is equal to hola, if it is then it will print "hi" to the console(and eventually be 'confirm' so that the user knows the answer) – Zeldacorp Sep 07 '13 at 01:09

1 Answers1

2

You should brush up on your understanding of variables and functions. You are doing two seemingly confused things.

Firstly, the following code sets all your variables to undefined, and so they all equal each other:

var hi;
var white;

var hola = hi;
var blanco = white;

You should being using statements like var hi = "hi";, which sets the value of the variable hi to the string "hi".

Secondly, when you invoke your translate function, you need to pass it a variable because you defined it as taking a variable word. So you would say translate("hi") for example. You also need to have your translate function actually do something. Right now, it just returns the same string that is passed into it.


The following is a solution, using the simpler techniques you should probably be familiar with based on the lesson. There are more complicated solutions for this kind of problem, but I assumed they are out of scope here.

Codeacademy does an excellent job teaching, so you should really go over the material again to understand what you were doing incorrectly. Good luck!

// English
var hi = "hi";
var white = "white";

// Spanish
var hola = "hola";
var blanco = "blanco";

var translate = function(word)
{
    // Spanish to English
    if (word === hola)
        console.log(hi);
    else if (word === blanco)
        console.log(white);
    // English to Spanish
    else if (word === hi)
        console.log(hola);
    else if (word === white)
        console.log(blanco);
};

// Ask for word
var word = prompt("Word to translate(lower case only):");
// Run translation
translate(word);

Demo: http://jsfiddle.net/W8eFp/1 (I have the demo using alert instead of console.log. You can click RUN up at the top to run the prompt again.)

Michal
  • 2,532
  • 1
  • 19
  • 27
  • Now it doesn't print out anything, maybe an answer with all the code(or most) written correctly; Also would you be able to explain how I can get this to work in eclipse? which I believe would then make it java and not javascript? – Zeldacorp Sep 07 '13 at 01:24
  • Awesome!, now the last thing is how can I make this a .exe or .jar in any way? or are my suspicions correct and im going to have to make this work for java in eclipse? – Zeldacorp Sep 07 '13 at 02:12
  • With JavaScript, no. You can put it in a web page or run it using `Node.js`, which is a runtime environment written in JavaScript. You would have to install `Node.js` to do so. As for Java - it is an entirely different language. But yes, you could write a similar program in Java. – Michal Sep 07 '13 at 02:14