skip to Main Content

I am new to learning how to code (1 week), so I have started with JavaScript. I have a question that I am trying to complete, and the details are as below;

‘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…)’

There are 10 tests that I need to pass and so far my code is passing 9 of them, I have tried everything for the last 1 hour and cannot come to a conclusion of what I am doing incorrect, my code is below with the error message

function sayHello(country, time) {
    let greeting;

if (country === 'Spain' && time < 12){
        greeting = 'buenos dias'
    } else if (country === 'Spain' && time < 24){
        greeting = 'buenas noches'
    } else if (country === 'Mexico' && time < 12){
        greeting = 'buenos dias'
    } else if (country === 'Mexico' && time < 24){
        greeting = 'buenas noches'
    } else if (country === 'France' && time < 12) {
        greeting = 'bon matin'
    } else if (country === 'France' && time < 24) {
        greeting = 'bon soir'
    } else if (country === 'Spain' || 'Mexico' || 'France' && time > 24){
        greeting = null
    }

    // Don't change code below this line
    return greeting;
}

Error Code Here
`Greeting should be correct for Spain in the morning

✓  Well done!

Greeting should be correct for Spain in the evening

✓  Well done!

Greeting should be null if the time is invalid in Spain

✓  Well done!

Greeting should be correct for Mexico in the morning

✓  Well done!

Greeting should be correct for Mexico in the evening

✓  Well done!

Greeting should be null if the time is invalid in Mexico

✕ AssertionError: expected ‘buenos dias’ to equal null

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!`

As you can see, everything is marked well done besides the assertionerror regarding Mexio

I am trying to get Mexico’s greeting to be NULL if the time is >24 also, similar to Spain and France, however, I cannot come to a conclusion as all the code is the same as Spain and France, except I have just written Mexico instead.

3

Answers


  1. Chosen as BEST ANSWER

    So after some more trying I managed to get 0 errors with the code below;

    function sayHello(country, time) {
    let greeting = null;
    if (country === 'Spain' || country === 'Mexico') {
        if (time >= 0 && time < 12) greeting = 'buenos dias';
        if (time >= 12 && time < 24) greeting = 'buenas noches';
    }
    if (country === 'France') {
        if (time >= 0 && time < 12) greeting = 'bon matin';
        if (time >= 12 && time < 24) greeting = 'bon soir';
    }
    // Don't change code below this line
    return greeting;
    

    }

    Still not 100% sure where my original error came from as it seemed to make sense to me, but I restarted and was able to find a solution.Thank you all for the help!


  2. It is difficult to pinpoint the error with the details you provided however my best guess is that it lies here

    It is morning if the time is 0 or more

    You don’t have any checks ensuring the time > 0. I would rearrange the order of the if‘s to validate time at the beginning like this:

    function sayHello(country, time) {
        let greeting;
        
        if (typeof time !== "number" || time < 0 || time > 24) {
            greeting = null
        } else if (country === 'Spain' && time < 12) {
            greeting = 'buenos dias'
        } else if (country === 'Spain' && time < 24) {
            greeting = 'buenas noches'
        } else if (country === 'Mexico' && time < 12) {
            greeting = 'buenos dias'
        } else if (country === 'Mexico' && time < 24) {
            greeting = 'buenas noches'
        } else if (country === 'France' && time < 12) {
            greeting = 'bon matin'
        } else if (country === 'France' && time < 24) {
            greeting = 'bon soir'
        }
    
        // Don't change code below this line
        return greeting;
    }
    
    Login or Signup to reply.
  3. Instead of writing country === 'Spain' || 'Mexico' || 'France', you should write country === 'Spain' || country === 'Mexico' || country === 'France'.

    The time check should be time >= 24 or time < 0 to include 24 and negative values as invalid times.

    function sayHello(country, time) {
        let greeting;
    
        if (country === 'Spain' && time < 12){
            greeting = 'buenos dias'
        } else if (country === 'Spain' && time < 24){
            greeting = 'buenas noches'
        } else if (country === 'Mexico' && time < 12){
            greeting = 'buenos dias'
        } else if (country === 'Mexico' && time < 24){
            greeting = 'buenas noches'
        } else if (country === 'France' && time < 12) {
            greeting = 'bon matin'
        } else if (country === 'France' && time < 24) {
            greeting = 'bon soir'
        } else if ((country === 'Spain' || country === 'Mexico' || country === 'France') && (time >= 24 || time < 0)){
            greeting = null
        } else {
            greeting = null
        }
    
        // Don't change code below this line
        return greeting;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search