1

Very new to learning Javascript and have super limited knowledge and am a bit lost. I'm creating a console based program that will basically translate a number into a different language. The number needs to be between 1 and 30, and can only be translated into French or German. I have added all of the French/German translations into two separate arrays, however I need to use a function to get my result (currently using 'if' statements, which is working fine but not what I need to do). I haven't added a function yet and this is my code (incomplete but hopefully you get the idea)

console.log("Start of the program");

var number= prompt ("Which number between 1 and 30 do you want to translate?");

if (number <1) {
    
alert("Please enter a number between 1 and 30");
    
var number= prompt ("Which number between 1 and 30 do you want to translate?");
    
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?");
    }
}


if (number => 1 && number <= 30) {
    
var lang= prompt ("Which language do you want to translate into, French or German?");
}

var frenchNumbers = ["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 germanNumbers = ["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"];


if (number == 1 && lang == "French") {
    alert(frenchNumbers[1]);
} 

This works perfectly fine, but I need it to do the same thing but using a function, and I just can't get my head around it. I need to follow this template:

function translate(number,lang){

//your code here;

return translatedNumber;

}

Any help would be greatly appreciated, Thanks, Brad

2
  • Wrap everything: function translate(number,lang){ [your huge blob of code here] [the last line code in the function template] } But also, change alert(frenchNumbers[1]) to return frenchNumbers[number - 1] and remove the if around it. If this isn't too clear, please tell me and I will update it. Commented Jun 2, 2021 at 5:26
  • Thanks for the quick reply. I only have the one result as I didn't want to go through and create 59 more if statements if it's going to be incorrect. I need the last line to say 'return translatedNumber': if (number == 1 && lang == "French") { alert(frenchNumbers[1]); } If I were to use this I would need to enter it another 59 times (all numbers 1-30 and in both French and German) Commented Jun 2, 2021 at 5:54

2 Answers 2

1

Assuming your frenchNumbers and germanNumbers array index wise numbers are arranged

Case 1: if you just have 2 languages, then a simple ternary operation will work

function translate(number,lang){

return lang === "French" ? frenchNumbers[number] : germanNumbers[number]

}

Case 2: If you have more than 2 languages or in future you may support more than 2 languages you can have switch-case

function translate(number,lang){

  switch(true)
  {
      case lang === "French":
           returnn frenchNumbers[number];
      case lang === "German":
           returnn germanNumbers[number];
      //case lang === "Arbic":
           //returnn arbicNumbers[number];
      default:
            return null;

  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick reply. Sorry I'm a complete beginner and don't understand your answer very clearly. It is just for an introduction to Javascript course so it will only be the two languages for now. For example if the user enters the number 4 and the language German, I need to have 'return translatedNumber' be 'Vier'. I'm just confused as to how to attach everything together. Will I need to go through and add another 59 statements like if (number == 1 && lang == "French") { alert(frenchNumbers[1]); } for all 30 numbers in both languages? Thanks
1

This works perfectly fine, but I need it to do the same thing but using a function, and I just can't get my head around it. I need to follow this template:

One of the things you can do is create a map of the numbers and the labels so you don't have to worry about the sequence of the array:

const french = [
  { number: 0, label: "Zéro" },
  { number: 1, label: "Un" },
  { number: 2, label: "Deux" },
  { number: 3, label: "Trois" },
]

function translateNumber(number, lang) {
  if(lang === "french") {
    return french.find(x => x.number === number);
  }
  
  return null;
}

console.log(translateNumber(1, "french"));

Explanation:

We have an array with objects that have a mapping of the english numbers with the french numbers. Arrays have a prototype method Array.find(...), which returns the first item in the array that matches the passed condition. Read more about Array.find

In the above code,

  1. We check which language it is: the lang parameter.
  2. Assuming lang=french, we then perform a .find operation on the french array i.e french.find(...).
  3. Inside the find function, we check that the object (x) includes the property number (x.number). If there is a match (x.number === number) we essentially return that object.
  4. If nothing matches or we have a lang that we don't support, the function will return null i.e. return null.

2 Comments

Thanks for the reply. Sorry I'm a complete beginner and we haven't covered anything like "return french.find(x => x.number === number);" yet so I don't quiet understand what this means/does. Could you explain a little bit more, and explain how I can use this as 'return translatedNumber' no matter which language it's in?
Updated the answer @Brad

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.