Reputation: 25
Kia ora, I'm am working on a problem that requires a function and array to be used in the answer. I'm a beginner!! The program needs to ask the user for a number between 1 and 30 that will get translated to either French or German. I've got it all working without using a function. I guess this is a little bit backwards, but now I need to get the function into the code. My understanding of them is beginner level, and there is something that I am missing despite watching countless videos. I'll attach what I have done with comments through it about my thinking. Thanks in advance for your help.
//write function to translate number into choosne language
function translate(number,lang){
//let phrase worked when I had it at the bottom of all the code, ie when there was no function!
let word = lang === "french"? french[number] : german[number];
//relates to function. I think I need tohave the above line return a value, this is what I
think this shold look like
return translatedNumber;
}
//array for French numbers 0-30
let french = ["zéro", "un", "deux", "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"];
//array for German numbers 0-30
let german = ["null", "eins", "zwei", "drei", "vier", "fünf", "sechs", "sieben", "acht",
"neun", "zehn", "elf", "zwölf", "dreizehn", "vierzehn", "fünfzehn", "sechszehn", "siebzehn",
"achtzehn", "neunzehn", "zwanzig", "einundzwanzig", "zweiundzwanzig", "dreiundzwanzig",
"vierundzwanzig", "fünfundzwanzig", "sechsundzwanzig", "siebenundzwanzig", "achtundzwanzig",
"neunundzwanzig", "dreiβig"];
//this block asks for number, gives message if number given is outside scoop. Started with if statement and got forever loop when answer was outside scoop. Changed to while and it works.
let number = prompt ("What number to translate? ");
while (number <= 0 || number > 30) {
alert ("Enter a number between 1 and 30.");
number = prompt ("What number to translate? ");
}
//while statement to ask tranlate langugae wanted. changes answer to lowercase so comparision works regardless of case that the user inputs.
let lang = prompt ("What language to translate? ");
while (lang.toLowerCase() !== "french" && lang.toLowerCase() !== "german") {
alert ("Enter french or german only");
lang = prompt ("What language to translate? ")
}
//I was thinking this would take the answer from the function and then use it in the alert to answer the users question.
let translatedNumber = translate(number, lang);
alert ("The number " + number + " is " + word + " in " + lang);
Upvotes: 0
Views: 97
Reputation: 1662
You are so close. In your function, just return word
, it already holds your translated number.
Then in the alert after you call your function, use your translatedNumber
instead of word
variable.
Also make sure that the string "dix-neug" is all on one line (you can't split a "" string).
See my EDIT comments for specific edits.
Run the code snippet to see that it works.
//write function to translate number into choosne language
function translate(number,lang){
//let phrase worked when I had it at the bottom of all the code, ie when there was no function!
let word = lang === "french"? french[number] : german[number];
//relates to function. I think I need tohave the above line return a value, this is what I
// think this shold look like
// EDIT Just return word, it already holds your translated Number
// return translatedNumber;
return word;
}
//array for French numbers 0-30
let french = ["zéro", "un", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf",
// EDIT The string "dix-neug" must all be on one line (you can't split a "" string)
"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"];
//array for German numbers 0-30
let german = ["null", "eins", "zwei", "drei", "vier", "fünf", "sechs", "sieben", "acht",
"neun", "zehn", "elf", "zwölf", "dreizehn", "vierzehn", "fünfzehn", "sechszehn", "siebzehn",
"achtzehn", "neunzehn", "zwanzig", "einundzwanzig", "zweiundzwanzig", "dreiundzwanzig",
"vierundzwanzig", "fünfundzwanzig", "sechsundzwanzig", "siebenundzwanzig", "achtundzwanzig",
"neunundzwanzig", "dreiβig"];
//this block asks for number, gives message if number given is outside scoop. Started with if statement and got forever loop when answer was outside scoop. Changed to while and it works.
let number = prompt ("What number to translate? ");
while (number <= 0 || number > 30) {
alert ("Enter a number between 1 and 30.");
number = prompt ("What number to translate? ");
}
//while statement to ask tranlate langugae wanted. changes answer to lowercase so comparision works regardless of case that the user inputs.
let lang = prompt ("What language to translate? ");
while (lang.toLowerCase() !== "french" && lang.toLowerCase() !== "german") {
alert ("Enter french or german only");
lang = prompt ("What language to translate? ")
}
//I was thinking this would take the answer from the function and then use it in the alert to answer the users question.
let translatedNumber = translate(number, lang);
// EDIT Use your translatedNumber instead of word variables
// alert ("The number " + number + " is " + word + " in " + lang);
alert ("The number " + number + " is " + translatedNumber + " in " + lang);
Upvotes: 0
Reputation: 3843
Easy! just wrap it in a function and call that function!
cut this code into translate()
//array for French numbers 0-30
let french = ["zéro", "un", "deux", "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"];
//array for German numbers 0-30
let german = ["null", "eins", "zwei", "drei", "vier", "fünf", "sechs", "sieben", "acht",
"neun", "zehn", "elf", "zwölf", "dreizehn", "vierzehn", "fünfzehn", "sechszehn", "siebzehn",
"achtzehn", "neunzehn", "zwanzig", "einundzwanzig", "zweiundzwanzig", "dreiundzwanzig",
"vierundzwanzig", "fünfundzwanzig", "sechsundzwanzig", "siebenundzwanzig", "achtundzwanzig",
"neunundzwanzig", "dreiβig"];
it belongs in that method, not outside of it.
finally put this last bit of code in its own method :
function startMethod(){
//this block asks for number, gives message if number given is outside scoop. Started with if statement and got forever loop when answer was outside scoop. Changed to while and it works.
let number = prompt ("What number to translate? ");
while (number <= 0 || number > 30) {
alert ("Enter a number between 1 and 30.");
number = prompt ("What number to translate? ");
}
//while statement to ask tranlate langugae wanted. changes answer to lowercase so comparision works regardless of case that the user inputs.
let lang = prompt ("What language to translate? ");
while (lang.toLowerCase() !== "french" && lang.toLowerCase() !== "german") {
alert ("Enter french or german only");
lang = prompt ("What language to translate? ")
}
//I was thinking this would take the answer from the function and then use it in the alert to answer the users question.
let translatedNumber = translate(number, lang);
alert ("The number " + number + " is " + word + " in " + lang);
}
then at the end, call your function!
startMethod();
Upvotes: 3