Introduction
In this lab, we will explore and practice JavaScript programming concepts and techniques. The lab is designed to provide hands-on experience in solving programming challenges and exercises, ranging from basic syntax and data types to more advanced topics such as functions, arrays, and object-oriented programming. By the end of the lab, you will have improved your JavaScript skills and be better equipped to tackle real-world programming tasks.
Checking if Date is a Weekend
To check if a given date is a weekend, follow these steps:
- Open the Terminal/SSH and type
nodeto start practicing coding. - Use
Date.prototype.getDay()method to get the day of the week as a number (0-6), with Sunday being 0 and Saturday being 6. - Check if the day is a weekend by using a modulo operator (
%) and comparing it to 0 or 6. - Omit the argument,
d, to use the current date as default.
Here's an example code snippet that you can use:
const isWeekend = (d = new Date()) => d.getDay() % 6 === 0;
To test the function, simply call it without any argument:
isWeekend(); // true or false (depending on the current date)
This will return true if the current date is a weekend (Saturday or Sunday) and false otherwise.
Summary
Congratulations! You have completed the Date Is Weekend lab. You can practice more labs in LabEx to improve your skills.