Hi I'm needing some help I'm pretty new to coding I can't seem to get my function to work it keeps returning undefined, any tips would be appreciated. thanks
var german = ["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 french = ["un","duex","trois","quatre","cinq","six","sept","huit","neuf","dix","onze","douze","treize","quatorze","quinze","seize","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 translatedNumber = translate();
var number = prompt("Please enter a number between 1-30 to be translated");
if (number > 30 || number < 1) {
alert("Please enter a number between 1-30")
exit()
}
else if(isNaN(number)){
alert("Please enter digits")
exit()
}
var language = prompt("Translated into what language? French/German");
if (language == "german" || language == "french"){
translate();
}
else{
alert("Only French or German is allowed")
exit()
}
function translate(number,language,translatedNumber,french,german){
if(language == "german"){
var translatedNumber = german[number - 1];
}
else if(language == "french"){
var translatedNumber = french[number - 1];
}
return translatedNumber;
}
alert("The translation is " translatedNumber);
CodePudding user response:
Fixed it:
(The problem was you didn't set up your function properly. I changed the translate function variables to lan and num since those are the only two you use in the function. Also I didn't think you needed to define the translatedNumber at the top yet since you have not used it yet) Hope this helps :D.)
var german = ["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 french = ["un","duex","trois","quatre","cinq","six","sept","huit","neuf","dix","onze","douze","treize","quatorze","quinze","seize","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 translatedNumber;
var number = prompt("Please enter a number between 1-30 to be translated");
if (number > 30 || number < 1) {
alert("Please enter a number between 1-30")
exit()
}
else if(isNaN(number)){
alert("Please enter digits")
exit()
}
var language = prompt("Translated into what language? French/German");
if (language == "german" || language == "french"){
translate(language, number);
}
else{
alert("Only French or German is allowed")
exit()
}
function translate(lan, num){
if(lan == "german"){
translatedNumber = german[num - 1];
} else if(lan == "french"){
translatedNumber = french[num - 1];
}
return translatedNumber;
}
alert("The translation is " translatedNumber);
CodePudding user response:
A few things to note about your code:
- The
exit
function is not defined. - Your
translatedNumber
is a global variable that is modified from within another function. Avoid such globals. - You should check whether a variable is a number first before checking whether it is within the required range.
Hope the following is what you are seeking:
const german = ["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"];
const french = ["un","duex","trois","quatre","cinq","six","sept","huit","neuf","dix","onze","douze","treize","quatorze","quinze","seize","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"];
try {
const number = prompt("Please enter a number between 1-30 to be translated");
if (isNaN(number))
throw({ message: "Please enter digits"});
if (number > 30 || number < 1)
throw({ message: "Please enter a number between 1-30"});
const language = prompt("Translated into what language? French/German");
if (language == "german" || language == "french"){
const translatedNumber = translate(number, language);
alert("The translation is " translatedNumber);
}
else
throw({ message: "Only French or German is allowed"});
} catch (error) {
alert(error.message);
}
function translate(number,language){
return language == "german" ? german[number - 1] : french[number - 1];
}