Conditional JS Challenge (条件性 JavaScript 挑战)

huangapple go评论70阅读模式
英文:

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 &gt;= 0 &amp;&amp; time &lt; 12 &amp;&amp; country === &#39;Spain&#39; || &#39;Mexico&#39;) {  
        greeting = &#39;buenos dias&#39;;
    } else if (time &gt;= 12 &amp;&amp; time &lt; 24 &amp;&amp; country === &#39;Spain&#39; || &#39;Mexico&#39;) {
        greeting = &#39;buenas noches&#39;;
    } else {
        greeting = null;
    }

    if (time &gt;= 0 &amp;&amp; time &lt; 12 &amp;&amp; country === &#39;France&#39;) {  
        greeting = &#39;bon matin&#39;;
    } else if (time &gt;= 12 &amp;&amp; time &lt; 24 &amp;&amp; country === &#39;France&#39;) {
        greeting = &#39;bon soir&#39;;
    } else {
        greeting = null;
    }
    // Don&#39;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 (&amp;&amp;) 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 &gt;= 0 &amp;&amp; time &lt; 12 &amp;&amp; country === &#39;Spain&#39;) || &#39;Mexico&#39;. 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 ([&#39;Spain&#39;, &#39;Mexico&#39;].includes(country)) {
    if (time &gt;= 0 &amp;&amp; time &lt; 12) {  
        greeting = &#39;buenos dias&#39;;
    } else if (time &gt;= 12 &amp;&amp; time &lt; 24) {
        greeting = &#39;buenas noches&#39;;
    } 
} else if (country === &#39;France&#39;) {
    if (time &gt;= 0 &amp;&amp; time &lt; 12) {  
        greeting = &#39;bon matin&#39;;
    } else if (time &gt;= 12 &amp;&amp; time &lt; 24) {
        greeting = &#39;bon soir&#39;;
    }
}

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 &#39;Spain&#39;:
    case &#39;Maxico&#39;:
        if (time &gt;= 0 &amp;&amp; time &lt; 12) {  
            greeting = &#39;buenos dias&#39;;
        } else if (time &gt;= 12 &amp;&amp; time &lt; 24) {
            greeting = &#39;buenas noches&#39;;
        } 
        break;
    case France:
        if (time &gt;= 0 &amp;&amp; time &lt; 12) {  
            greeting = &#39;bon matin&#39;;
        } else if (time &gt;= 12 &amp;&amp; time &lt; 24) {
            greeting = &#39;bon soir&#39;;
        }
        break;
    default:
        greeting = null;
}

huangapple
  • 本文由 发表于 2023年5月20日 20:50:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定