gpt4 book ai didi

javascript - JavaScript 中的函数 - 初学者问题

转载 作者:行者123 更新时间:2023-12-05 02:27:13 24 4
gpt4 key购买 nike

起亚奥拉,我正在处理一个需要在答案中使用函数和数组的问题。我是初学者!!该程序需要向用户询问一个 1 到 30 之间的数字,该数字将被翻译成法语或德语。我已经在不使用函数的情况下完成了所有工作。我想这有点倒退,但现在我需要将函数放入代码中。我对它们的理解是初学者水平,尽管看了无数视频,但我仍然缺少一些东西。我会附上我所做的事情,并通过它评论我的想法。预先感谢您的帮助。

//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);

最佳答案

简单!只需将其包装在一个函数中并调用该函数即可!

把这段代码剪成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"];

它属于那个方法,而不是它之外。

最后把这最后一段代码放在它自己的方法中:

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);

}

然后在最后,调用您的函数!

startFunction();

关于javascript - JavaScript 中的函数 - 初学者问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73340231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com