I'm trying some challenges on northcoders and I've been trying this problem for ages.
this is the question:
**A bunch of Northcoders are planning their holidays, and are hoping to use this code to help them practice greetings in the language of the country they are visiting.
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...)
Just a reminder: you can use country and time in expressions just like any other variable. There are lots of ways you could solve this challenge! Use any tools you've learned so far.**
my answer is this:
function sayHello(country, time) {
let greeting;
switch (country) {
case 'Spain':
if (time >= 0 && time < 12) {
greeting = 'buenos dias';
}if (time >= 12 && time < 24) {
greeting = 'buenas noches';
}else if(time >=24){
greeting = null;
}
break;
case 'Mexico':
if (time >= 0 && time < 12) {
greeting = 'buenos dias';
}if (time >= 12 && time < 24) {
greeting = 'buenas noches';
}else if(time >=24){
greeting = null;
}
break;
case 'France':
if (time >= 0 && time < 12){
greeting = 'bon matin';
}if (time >= 12 && time < 24){
greeting = 'bon soir';
}else if(time >=24){
greeting = null;
}
break;
default:
greeting = null;
}
// Don't change code below this line
return greeting;
}
this is the result I'm getting but I don't understand where it's going wrong:
9 Passing 2 Failing
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 undefined 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 if the time is invalid in France
✕ AssertionError: expected undefined to equal null
Greeting should be null for other countries
✓ Well done!
Without seeing the input, my guess is that the input is a negative number on the cases that are failing. You're code doesn't handle that at all. I would change your code so that it handles all possible input. I would recommend refactoring to something like this:
function sayHello(country, time) {
let greeting;
let morning;
let evening;
switch (country) {
case 'Spain':
morning = 'buenos dias'
evening = 'buenas noches';
break;
case 'Mexico':
morning = 'buenos dias'
evening = 'buenas noches';
break;
case 'France':
morning = 'bon matin';
evening = 'bon soir';
break;
default:
morning = null;
evening = null;
}
if (time >= 0 && time < 12) {
greeting = morning;
} else if (time >= 12 && time < 24) {
greeting = evening;
} else {
greeting = null;
}
// Don't change code below this line
return greeting;
}