gpt4 book ai didi

javascript - JS中不同语言打印 "welcome"

转载 作者:行者123 更新时间:2023-11-28 15:06:59 24 4
gpt4 key购买 nike

任务是根据所选语言打印一条问候消息。我得到了一个对象文字。例如,如果用户选择“dutch”,程序将打印“Welkom”等。如果未找到语言,程序应打印默认语言,即英语。我已经能够解决大部分问题,除了一件事,现在在当前代码中,当找到一种语言时,它还会用英语打印一条欢迎消息,我错过了什么?

var o = {
english: 'Welcome',
czech: 'Vitejte',
danish: 'Velkomst',
dutch: 'Welkom',
estonian: 'Tere tulemast',
finnish: 'Tervetuloa',
flemish: 'Welgekomen',
french: 'Bienvenue',
german: 'Willkommen',
irish: 'Failte',
italian: 'Benvenuto',
latvian: 'Gaidits',
lithuanian: 'Laukiamas',
polish: 'Witamy',
spanish: 'Bienvenido',
swedish: 'Valkommen',
welsh: 'Croeso'
}

function GetLang(arg) {

for (key in o) {
if (arg === key) {
console.log(o[key])
}
}
if (arg !== key) {
console.log(o.english)
}

}
GetLang('danish');

最佳答案

您可以直接使用参数arg,并用它来检查该属性是否在数组中。然后您将获得该值,否则您将获得 english 属性的值。

return o[arg] || o.english;

var o = {
english: 'Welcome',
czech: 'Vitejte',
danish: 'Velkomst',
dutch: 'Welkom',
estonian: 'Tere tulemast',
finnish: 'Tervetuloa',
flemish: 'Welgekomen',
french: 'Bienvenue',
german: 'Willkommen',
irish: 'Failte',
italian: 'Benvenuto',
latvian: 'Gaidits',
lithuanian: 'Laukiamas',
polish: 'Witamy',
spanish: 'Bienvenido',
swedish: 'Valkommen',
welsh: 'Croeso'
};

function GetLang(arg) {
return o[arg] || o.english;
}

console.log(GetLang('danish'));

关于javascript - JS中不同语言打印 "welcome",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38479110/

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