Home > Mobile >  java script code to translate numbers into French or German
java script code to translate numbers into French or German

Time:11-28

I am required to write a translating code for translating numbers into French and German only. I have created the arrays for the french, numbers, array for the German numbers, and defined my number ranges using the boolean expression: number=>1 &&<=30. I need the code to write in the function translate

Here is my attempted code

    function translate(number,lang)
    {var number= prompt ("Enter text to translate?");
    undefined
    if (number <1 && number > 30) {
    alert("Please enter a number between 1 and 30");

    var number= prompt ("Please enter an integer btween 1 and 30");
    if (number >30) {
        alert("Please enter a number between 1 and 30");
        var number= prompt ("Which number between 1 and 30 do you want to 
    translate?");
    
    }
    }
    undefined
    if (number <= 1 && number <= 30) 
    var lang= prompt ("Please enter translation language, French or 
    German?");

    if (lang==french & lang ==german);{var french = ["Zéro", "Un", "Deux", 
    "Trois", "Quatre", "Cinq", "Six", "Sept", "Huit", "Neuf", "Dix", "Onze", 
    "Douze", "Treize", "Quatorze",
    "Quinze", "Sieze", "Dix-sept", "Dix-huit", "Dix-neuf", "Vingt", "Vingt 
    Et Un", "Vingt-deux", "Vingt-trois", "Vingt-quatre", "Vingt-cinq", 
    "Vingt-six", "Vingt-sept", "Vingt-huit", "Vingt-neuf", "Trente"];

    var german = ["Null", "Eins", "Zwei", "Drei", "Vier", "Fünf", "Sechs", 
    "Sieben", "Acht", "Neun", "Zehn", "Elf", "Zwölf", "Dreizehn", 
    "Vierzehn", "Fünfzehn", 
    "Sechzehn", "Siebzehn", "Achtzehn", "Neunzehn", "Zwanzig", 
    "Einundzwanzig", "Zweiundzwanzig", "Dreiundzwanzig", "Vierundzwanzig", 
    "Fünfundzwanzig", 
    "Sechsundzwanzig", "Siebenundzwanzig", "Achtundzwanzig", 
    "Neunundzwanzig", "Dreiβig"];
    break;
    }
    }   

   {prompt ("only French and German allowed")
     return lang === "French" ? french[number] : german[number]}

here are the instructions I am trying to code above

The software will translate into French or German integer numbers from 1 to 30 inclusive. The program should ask:

  1. What number to translate • The user should type digits • If the user doesn’t type digits, the following message must be displayed “Please use digits” and the program must end • If the user doesn’t type a number between 1 and 30, the following message must be displayed “Please type an integer number between 1 and 30” and the program must end
  2. What is the output language • The user should type German or French • If the user doesn’t type German or French, the following message must be displayed “only French or German is allowed” and the program must end

CodePudding user response:

I read your code and I guess I have found some bugs... First of all as @connexo mentioned in the comments the second condition in your if is useless :

if (number <= 1 && number <= 30) 

If a number already is less than or equal to 1, it's always less than 30.

I think what you wanted to do is to check if the number is between 1 and 30 so the correct condition set would be :

if (number >= 1 && number <= 30) 

Second, you defined a lang variable in your function arguments and then defined another variable with the same name in the body of your function. Be more careful about your variables name and try to use unique values for your variables. Anyway I couldn't get this part of your code either :

if (lang==french & lang ==german);

so lang should be both french and german ? and what is the type of lang? if its of type string you should have used "" or ''. And also I don't get the ; at the end of the if.

The first if is also incorrect, if you want to check a number is between to numbers you should use || operator not && because it is impossible for a number to be smaller than 1 and greater than 30 at the same time.

if (number <1 || number > 30)

So I reviewed your code and improved it a bit and checked it so its fully functional and gets the job done, but you have to find a way to implement it in your application

  var french = ["Zéro", "Un", "Deux",
    "Trois", "Quatre", "Cinq", "Six", "Sept", "Huit", "Neuf", "Dix", "Onze",
    "Douze", "Treize", "Quatorze",
    "Quinze", "Sieze", "Dix-sept", "Dix-huit", "Dix-neuf", "Vingt", "Vingt Et Un", "Vingt - deux", 
    "Vingt - trois", "Vingt - quatre", "Vingt - cinq",
    "Vingt-six", "Vingt-sept", "Vingt-huit", "Vingt-neuf", "Trente"
  ];

  var german = ["Null", "Eins", "Zwei", "Drei", "Vier", "Fünf", "Sechs",
    "Sieben", "Acht", "Neun", "Zehn", "Elf", "Zwölf", "Dreizehn",
    "Vierzehn", "Fünfzehn",
    "Sechzehn", "Siebzehn", "Achtzehn", "Neunzehn", "Zwanzig",
    "Einundzwanzig", "Zweiundzwanzig", "Dreiundzwanzig", "Vierundzwanzig",
    "Fünfundzwanzig",
    "Sechsundzwanzig", "Siebenundzwanzig", "Achtundzwanzig",
    "Neunundzwanzig", "Dreiβig"
  ];

  var number = parseInt(prompt("Enter number to translate?"));

  while (number < 0 || number > 30) {
    alert("Please enter a number between 1 and 30");
    number = prompt("Please enter an integer between 1 and 30");
  }

  var lang = prompt("Please enter translation language, French or German ? ");

  while(lang != "french" && lang != "german")
  {
    alert("Please enter a valid language (french or german)");
    lang = prompt("Please enter translation language, French or German ? ");
  }

var word =  lang === "french" ? french[number] : german[number];
alert("the number "   number  " in "   lang " is "  word);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

A few thing to remember :

  1. It's good practice for translating app and etc. to declare the dictionaries before the app gets executed
  2. Always ensure you are comparing the correct data types for example the prompt function returns an string but you were treating it like a number. (It's best to check the types and cast them to one another before comparison even though the language handle these things automatically)
  3. I found out that the dictionaries start from zero so I replaced 1s with 0s
  4. When declaring function arguments please ask yourself what are they intended to do or hold. Here you have defined two arguments and declared them again in your function... this is not good. If your translate function is in charge of translation only you should have validate the data before calling this function and then pass them to translate function.
  5. For validating data it's common that we use while loops... in this way we make sure of as long as the user is entering the incorrect data we do not proceed to the other part of our code and that is what we want exactly

CodePudding user response:

Use an object i18n with keys german and french for the lookup. Define it outside of the function, there's no need to re-declare it every time the function runs.

Use while loops to ensure the user enters what you need.

You can then do the lookup like this:

i18n[lang][number]

const i18n = {
  french: ["Zéro", "Un", "Deux", "Trois", "Quatre", "Cinq", "Six", "Sept", "Huit", "Neuf", "Dix", "Onze", "Douze", "Treize", "Quatorze", "Quinze", "Sieze", "Dix-sept", "Dix-huit", "Dix-neuf", "Vingt", "Vingt Et Un ", "Vingt - deux ", "Vingt-trois ", "Vingt-quatre ", "Vingt-cinq ", "Vingt-six", "Vingt-sept", "Vingt-huit", "Vingt-neuf", "Trente"],
  german: ["Null", "Eins", "Zwei", "Drei", "Vier", "Fünf", "Sechs", "Sieben", "Acht", "Neun", "Zehn", "Elf", "Zwölf", "Dreizehn", "Vierzehn", "Fünfzehn", "Sechzehn", "Siebzehn", "Achtzehn", "Neunzehn", "Zwanzig", "Einundzwanzig", "Zweiundzwanzig", "Dreiundzwanzig", "Vierundzwanzig", "Fünfundzwanzig", "Sechsundzwanzig", "Siebenundzwanzig", "Achtundzwanzig", "Neunundzwanzig", "Dreiβig"]
};

function translate(number, lang) {
  while (isNaN(number) || number < 0 || number > 30) { // keep asking while the user doesnt enter a valid number
    number = Number(prompt("Please enter a number from 0 to 30")); // What a user enters is always a string so we cast to Number
  }
  lang = typeof lang === 'string' ? lang.toLowerCase() : lang; // make sure we can disregard case in function argument
  while (lang !== 'french' && lang !== 'german') {
    lang = prompt("Please enter translation language, French or German?").toLowerCase(); // we want the user to enter the language without having to conform to case;
  }

  console.log(`The translation of ${number} to ${lang} is: ${i18n[lang][number]}`);
}

translate(5, 'German');
translate(29, 'freNch');
translate();
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related