1

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 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for the answer, I now understand the issue, however just to try I used your example and it failed, I guess it's a format they're not expecting but if not this way then I still have no idea how to write it how they want it. Also, I think switch statements are allowed, would it be possible to see what that would look like? Thanks again
@James regarding the failure - I had a typo there (missing ), already edited and fixed) - that was probably the issue. Regarding using a switch - see my edited answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.