0

I am undertaking an entry challenge for a bootcamp and I'm stuck on a particular task, I have tried many ways to complete the task and below is the closest I have come. The code is repeated and passes on 9 out of 10 aspects, but fails on 1. any help would be amazing.

The Task: The code 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.

My Code:

function sayHello(country, time) {
  let greeting = null

  switch (country) {
    case 'Mexico':
      if (time < 12) {
        greeting = 'buenos dias';
      } else if (time < 24) {
        greeting = 'buenas noches';
      }
      break;
    case 'Spain':
      if (time < 12) {
        greeting = 'buenos dias';
      } else if (time < 24) {
        greeting = 'buenas noches';
      }
      break;

    case 'France':
      if (time < 12) {
        greeting = 'bon matin';
      } else if (time < 24) {
        greeting = 'bon soir';
      }
      break;
    default:
      greeting = null;
  }
  // Don't change code below this line
  return greeting;
}

Errors: Greeting should be null if the time is invalid in Mexico ✕ AssertionError: expected 'buenos dias' to equal null

9
  • 3
    you have "Mexico" spelled as "Maxico" so that might be a problem Commented Jun 16, 2023 at 14:23
  • Well now i feel stupid. getting this error now: Greeting should be null if the time is invalid in Mexico ✕ AssertionError: expected 'buenos dias' to equal null Commented Jun 16, 2023 at 14:39
  • It tells you what the problem is: you need to validate the time parameter, as it states in the instructions: "If time is any other value, greeting should always be null, whatever the language." Commented Jun 16, 2023 at 15:10
  • 1
    e.g. what happens if I pass in time as -999 Commented Jun 16, 2023 at 15:10
  • As a side note, the cases Spain and Mexico behave in the same exact same way therefore you can group them Commented Jun 16, 2023 at 15:31

1 Answer 1

0

Following your request of clarification about why/how your solution works (or, better, seems to work) please compare the following snippets:

This is what I think you have done:

function sayHello(country, time) {
  let greeting = null

  switch (country) {
    case 'Mexico':
      if (time >= 0 && time < 12) {
        greeting = 'buenos dias';
      } else if (time >= 12 && time < 24) {
        greeting = 'buenas noches';
      }
      break;
    case 'Spain':
      if (time < 12) {
        greeting = 'buenos dias';
      } else if (time < 24) {
        greeting = 'buenas noches';
      }
      break;

    case 'France':
      if (time < 12) {
        greeting = 'bon matin';
      } else if (time < 24) {
        greeting = 'bon soir';
      }
      break;
    default:
      greeting = null;
  }
  // Don't change code below this line
  return greeting;
}

// test
console.log(`sayHello('Mexico', -9): ${sayHello('Mexico', -9)}`);
console.log(`sayHello('Spain', -9): ${sayHello('Spain', -9)} ATTENTION! You handled the issue only for 'Mexico'`);

As you can see, it is not a real fix: it is enough to pass the test but if you try to call the function with an invalid date for a different country, you still have the problem.

This is a potential solution with "minimal" changes to your code:

function sayHello(country, time) {
  // if the time is not valid return null early
  if (time < 0 || time >= 24) {
    return null;
  }
  
  // from here there is no change whatsoever to your code
  let greeting = null

  switch (country) {
    case 'Mexico':
      if (time < 12) {
        greeting = 'buenos dias';
      } else if (time < 24) {
        greeting = 'buenas noches';
      }
      break;
    case 'Spain':
      if (time < 12) {
        greeting = 'buenos dias';
      } else if (time < 24) {
        greeting = 'buenas noches';
      }
      break;

    case 'France':
      if (time < 12) {
        greeting = 'bon matin';
      } else if (time < 24) {
        greeting = 'bon soir';
      }
      break;
    default:
      greeting = null;
  }
  // Don't change code below this line
  return greeting;
}

// test
console.log(`sayHello('Spain', 13): ${sayHello('Spain', 13)}`);
console.log(`sayHello('spain', 13): ${sayHello('spain', 13)} - ATTENTION! 'spain' is not the same as 'Spain'`);
console.log(`sayHello('France', 0): ${sayHello('France', 0)}`);
console.log(`sayHello('Mexico', '5'): ${sayHello('Mexico', '5')}`);
console.log(`sayHello('Argentina', 13): ${sayHello('Argentina', 13)}`);
console.log(`sayHello('Mexico', -9): ${sayHello('Mexico', -9)}`);
console.log(`sayHello('Spain', -9): ${sayHello('Spain', -9)}`);
console.log(`sayHello('France', 25): ${sayHello('France', 25)}`);

By checking the time is valid (and returning null early, if it is not) you handle the issue for all the countries. Anyway you can easily stumble into different problems because of the lack of input validation/handling.

This is a slightly more optimized solution that also take into account different ways user can pass the country parameter:

function sayHelloOptimized(country, time) {
  // check if time is valid, if it is not, return null
  const _time = parseInt(time); // in case time is provided as a string ('10') instead of a number (10). It also makes the next `if` condition more compact.
  if (
    isNaN(_time) || // time is not a number or a digit-only string (e.g. 'foo', {}, [], null, undefined)
    _time < 0 || _time >= 24 // time is below 0 or equal to or above 24
  ) {
    return null;
  }

  // as you already took care of invalid values for _time, you can safely assume
  // that if _time is below 12 it is morning, otherwise it is evening
  const isMorning = _time <= 12 ? true : false;

  // Now you already know the kind of greeting you need to show and you just 
  // have to check the country, in order to use the appropriate translation.
  // You can use `toLowerCase()` in order to take into account people
  // inserting the country in different ways (e.g. 'Spain', 'spain', 'SPAIN')
  switch (country.toLowerCase()) {
    // as the return value is the same for Spain and Mexico, you can group the two cases
    case 'spain':
    case 'mexico':
      return isMorning ? 'buenos dias' : 'buenas noches';
    case 'france':
      return isMorning ? 'bon matin' : 'bon soir';
    default:
      return null;
  }
}

// test
console.log(`sayHelloOptimized('spain', 13): ${sayHelloOptimized('spain', 13)}`);
console.log(`sayHelloOptimized('France', 0): ${sayHelloOptimized('France', 0)}`);
console.log(`sayHelloOptimized('MEXICO', '5'): ${sayHelloOptimized('MEXICO', '5')}`);
console.log(`sayHelloOptimized('Argentina', 13): ${sayHelloOptimized('Argentina', 13)}`);
console.log(`sayHelloOptimized('Spain'): ${sayHelloOptimized('Spain')}`);
console.log(`sayHelloOptimized('MEXICO', 'foo'): ${sayHelloOptimized('MEXICO', 'foo')}`);
console.log(`sayHelloOptimized('france', 25): ${sayHelloOptimized('france', 25)}`);

Sign up to request clarification or add additional context in comments.

Comments

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.