英文:
Conditional JS Challenge
问题
以下是您提供的代码的中文翻译:
function sayHello(country, time) {
let greeting;
if (time >= 0 && time < 12 && (country === 'Spain' || country === 'Mexico')) {
greeting = 'buenos dias';
} else if (time >= 12 && time < 24 && (country === 'Spain' || country === 'Mexico')) {
greeting = 'buenas noches';
} else if (time >= 0 && time < 12 && country === 'France') {
greeting = 'bon matin';
} else if (time >= 12 && time < 24 && country === 'France') {
greeting = 'bon soir';
} else {
greeting = null;
}
return greeting;
}
请注意,我已经更正了条件语句,以确保它们按预期工作。主要问题是在条件语句中的括号和逻辑运算符的使用上。这应该能够解决您遇到的问题。
英文:
I am currently trying to pass a little challenge on a website, it's basically asking me to write conditional statements to make sure the time is morning and evening between certain times and also that if it is a certain country at that time it needs to be certain greetings. I will add the code here and a pasted snipped of the challenge.
What is frustrating is that it is saying that I have it correct for the France one but not the other two which I don't understand as I have done them all exactly the same so I don't know how one can be correct while the rest are wrong, but I'm more than likely missing something.
>The code you write should assign a value to greeting
that is correct depending on the country that is being visited and the time of day.
>It is morning if the time is 0 or more, but less than 12. If the time is 12 or more, but less than 24, it is evening. If time is any other value, greeting should always be null, whatever the language.
>If country is Spain or Mexico, greeting should be buenos dias in the morning and buenas noches in the evening. If country is France, greeting should be bon matin in the morning and bon soir in the evening. If country is any other value, greeting should always be null, whatever the time (we don't have many languages in our dictionary yet...)
function sayHello(country, time) {
let greeting;
if (time >= 0 && time < 12 && country === 'Spain' || 'Mexico') {
greeting = 'buenos dias';
} else if (time >= 12 && time < 24 && country === 'Spain' || 'Mexico') {
greeting = 'buenas noches';
} else {
greeting = null;
}
if (time >= 0 && time < 12 && country === 'France') {
greeting = 'bon matin';
} else if (time >= 12 && time < 24 && country === 'France') {
greeting = 'bon soir';
} else {
greeting = null;
}
// Don't change code below this line
return greeting;
}
And the errors I'm getting:
>6 Passing
>4 Failing
>Greeting should be correct for Spain in the morning
✕ AssertionError: expected null to equal 'buenos dias'
>Greeting should be correct for Spain in the evening
✕ AssertionError: expected null to equal 'buenas noches'
>Greeting should be null if the time is invalid in Spain
✓ Well done!
>Greeting should be correct for Mexico in the morning
✕ AssertionError: expected null to equal 'buenos dias'
>Greeting should be correct for Mexico in the evening
✕ AssertionError: expected null to equal 'buenas noches'
>Greeting should be null if the time is invalid in Mexico
✓ Well done!
>Greeting should be correct for France in the morning
✓ Well done!
>Greeting should be correct for France in the evening
✓ Well done!
>Greeting should be null if the time is invalid in France (remembering that 24 is an invalid time)
✓ Well done!
>Greeting should be null for other countries
✓ Well done!
Any help with this would be greatly appreciated, even if it's not the direct answer just to point me in the right direction as to what it is I'm clearly not getting here.
答案1
得分: 1
以下是您要翻译的内容:
"First, you have two unrelated series of if-else conditions. The first series attempts to handle Spain and Mexico, and regardless of its result, the greeting is then overwritten by a series of conditions handling France."
"Handling both of those issues could look like this:"
"Using a swtich
statement may be more elegant here, but I'm not sure if this is allowed by the instructions. If it is allowed, it could perhaps be a more elegant way of handling the countries:"
英文:
There are a few issues here.
First, you have two unrelated series of if-else conditions. The first series attempts to handle Spain and Mexico, and regardless of its result, the greeting is then overwritten by a series of conditions handling France.
The second issue is that the first condition probably doesn't do what you intended - it always evaluates to true. The logical and (&&
) operator has a higher precedence than the logical or (||
) operator, so if we'll add parentheses to clarify the order of evaluation, well get the condition (time >= 0 && time < 12 && country === 'Spain') || 'Mexico'
. Regardless of the what the left hand side evaluates to, the right hand side is a truth-y, so the entire condition evaluates to true.
Handling both of those issues could look like this:
let greeting = null;
if (['Spain', 'Mexico'].includes(country)) {
if (time >= 0 && time < 12) {
greeting = 'buenos dias';
} else if (time >= 12 && time < 24) {
greeting = 'buenas noches';
}
} else if (country === 'France') {
if (time >= 0 && time < 12) {
greeting = 'bon matin';
} else if (time >= 12 && time < 24) {
greeting = 'bon soir';
}
}
Side note:
Using a swtich
statement may be more elegant here, but I'm not sure if this is allowed by the instructions. If it is allowed, it could perhaps be a more elegant way of handling the countries:
switch (country) {
case 'Spain':
case 'Maxico':
if (time >= 0 && time < 12) {
greeting = 'buenos dias';
} else if (time >= 12 && time < 24) {
greeting = 'buenas noches';
}
break;
case France:
if (time >= 0 && time < 12) {
greeting = 'bon matin';
} else if (time >= 12 && time < 24) {
greeting = 'bon soir';
}
break;
default:
greeting = null;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论