0

Does anyone help me to solve the "Days Of The Week Exercise" in JavaScript? I searched MDN and tried many times as much as I can, but I still don't get what the null is, and how to use null.

if the argument(num) is less than 1 or greater than 7, the function should return null.

Days Of The Week Exercise

const days = ['Monday','Tuesyday','Wednesday','Thursday','Friday','Saturday','Sunday'];

let returnDay = (num) => {
    if (1 <= num <= 7) {
        return (days[num -1]);
    } else {
        return null;
    }
    
};

returnDay(1); // Monday
returnDay(7); // Sunday
returnDay(4); // Thursday
returnDay(0); // null
6
  • 4
    Why not upload images of code/errors when asking a question? Commented Nov 29, 2020 at 1:20
  • Please paste your code into your question :) Commented Nov 29, 2020 at 1:23
  • Well the issue isnt return null; as that is valid, null is just a normal value with a few weird evaluation properties Commented Nov 29, 2020 at 1:24
  • I'm really sorry for the inconvenience. I pasted a screenshot image, so I thought it is enough. But, I'm gonna paste my code directly for the future question!! Commented Nov 29, 2020 at 1:28
  • Wait - your screenshot and code differs - does the last function return undefined or null? Commented Nov 29, 2020 at 1:32

2 Answers 2

1

1 <= num <= 7 does not do what you think it does. In JavaScript, binary operators are evaluated from left to right. So 1 <= num <= 7 is really (1 <= num) <= 7, which is either true <= 7 or false <= 7 (depending on what num is). Either way, it will evaluate to true, but don’t ask me why, because you really shouldn’t compare booleans with numbers anyway. Since the condition is always true, the else branch is never executed, so you never get null.

I think what you want to write is:

if (1 <= num && num <= 7) {
   ...
} else {
   ...
}

Furthermore, remember that array indices start with 0. In your example, days[1] would be 'Tuesday', not 'Monday'.

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

2 Comments

I tried to change (1 <= num <= 7) to (1 <= num && num <= 7) and it worked just what I wanted!!! Thank you for your help!!! XD
@natsu_0 You're very welcome. Be sure to read the explanation and understand why it works. It will help you become a better programmer.
0

I hope this answer helps you :)

const days = ['Monday', 'Tuesyday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];

const returnDay = (num) => {
  const day = num - 1;
  if (days[day]) { // since you passing 0 there is no such element in the array at index -1,
                  //so if statement essentially evaluates to false and returning null right away;
    return days[day];
  }
  return null;
};

console.log(returnDay(1)); // Monday
console.log(returnDay(7)); // Sunday
console.log(returnDay(4)); // Thursday
console.log(returnDay(0)); // null

1 Comment

I didn't think of using "days[day]" for the if statement... Your answer helped me a lot to find a new way of writing!!! Thank you for your help!!

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.