Home > Software engineering >  How can I get my datepicker javascript code to tell me when a month has a Friday the 13th?
How can I get my datepicker javascript code to tell me when a month has a Friday the 13th?

Time:12-22

I am a complete beginner and confused how I can fulfill this task. It's the first task that has anything with true and false in the instructions. I can deal with the CSS and html we've been doing but I can't seem to wrap my head around JavaScript.

Instructions: "Use getFullMonth() to get the list of days. Note that now it does not matter what the number of the month is. From the resulting list of days, test whether the 13th day is Friday. Depending on the test, return true or false. Note: Because the function returns a boolean (true or false), the function has been named as a claim (“has Friday 13th”) which is a statement that can only be true or false. This makes it easier later to know what the function does and returns. For example, an if statement becomes easy to understand: if (monthHasFriday13th("Wednesday") === true) { ... }"

Here is my JavaScript code.

const weekdays = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];

const months = [
    { name: "January", days: 31 },
    { name: "February", days: 28 },
    { name: "March", days: 31 },
    { name: "April", days: 30 },
    { name: "May", days: 31 },
    { name: "June", days: 30 },
    { name: "July", days: 31 },
    { name: "August", days: 31 },
    { name: "September", days: 30 },
    { name: "October", days: 31 },
    { name: "November", days: 30 },
    { name: "December", days: 31 }
];



//Get Next Weekday!
  function getNextWeekday(day) {
      let i = 0;
        while (i < weekdays.length) {
                  if (day === weekdays[i]) {
                          if (i == weekdays.length - 1) {
                                  return weekdays[0];
                          }
                          else {
                                  return weekdays[i   1];
                          }
                  }
                  i = i   1;
          }
  }
  console.log("The next weekday is "   weekdays   "!")

//Get Full month!

function getFullMonth(monthNum,day1Weekday) {

    if (monthNum < 1 || monthNum > 12) {
                return null;
    }
        if ( weekdays.indexOf(day1Weekday) === -1 ) {
                return null;
        }

            let outcome = [];
            monthNum = monthNum - 1;
            let currentDay = day1Weekday;
            for (let i = 0 ; i < months[monthNum].days ; i = i   1 ) {
                let nextDay = {
                        dayNum: i   1,
                        dayName: currentDay
                };
                outcome.push(nextDay);
                currentDay = getNextWeekday(currentDay);
        }
return outcome;
}

//Get Fridays 13th!
function monthHasFriday13th(????????){

?????????????!???

}

I tried to figure out how to reference the previous function, but it is a rather pathetic attempt considering that I haven't done this before either and I'm not even sure what about the function getFullMonth(monthNum,day1Weekday) is supposed to help with this.

This...

fundction monthHasFriday13th(day1weekday){
let 
if ( getFullMonth (monthNum,day1Weekday) = (,13) )

if (getFullMonth (monthNum,day1Weekday) == 13 ){
  nameFound = true;

  return ()  
}

CodePudding user response:

The only information you need to determine if a month has a friday the 13th is to see if the first day is Sunday.

So the function "monthHasFridayThe13th()" should take one argument- the month object that is returned by getFullMonth(). You should then check the first object in the array (position 0) and see if the day is 'Sunday'. That is all.

function monthHasFriday13th(month){
  return month[0].dayName === "Sunday";
}
let October = getFullMonth(10,"Sunday");
let November = getFullMonth(11,"Friday");
console.log(monthHasFriday13th(October)); // true
console.log(monthHasFriday13th(November)); // false

CodePudding user response:

The getFullMonth function returns an list of days (ie.. ["Monday", "Tuesday", ...])

The size of this list depends on how many days are in the specified month "monthNum". The first day in the list is "day1Weekday"

Once this list is constructed, you can simply check the 12th position in the list is "Friday". The reason you check the 12th position is because the array starts with position of "0"... which means the 12th position is the actual 13th day.

Run the code snippet to see the code in action...

const weekdays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
const months = [
  { name: "January", days: 31 },
  { name: "February", days: 28 },
  { name: "March", days: 31 },
  { name: "April", days: 30 },
  { name: "May", days: 31 },
  { name: "June", days: 30 },
  { name: "July", days: 31 },
  { name: "August", days: 31 },
  { name: "September", days: 30 },
  { name: "October", days: 31 },
  { name: "November", days: 30 },
  { name: "December", days: 31 }
];

function getFullMonth(monthNum,day1Weekday) {
  if(monthNum < 1 || monthNum > 12) return [];
  if(!weekdays.includes(day1Weekday)) return [];
  
  let daysInMonth = months[monthNum - 1].days;
  let curIndex = weekdays.findIndex(x => x == day1Weekday) - 1;
  
  let listOfDays = [];
  for(let n = 0; n < daysInMonth; n  ){
    curIndex = (curIndex   1) % 7;
    listOfDays.push(weekdays[curIndex])
  }
  return listOfDays;
}

function monthHasFriday13th(monthNum,day1Weekday){
  let listOfDays = getFullMonth(monthNum,day1Weekday);

  if(listOfDays.length && listOfDays[12] == "Friday") return true;
  return false;  
}

console.log(monthHasFriday13th(1, "Sunday"));

CodePudding user response:

The function getFullMonth generates the days for the whole month, based on the parameters which specifies the month number and first day of the month. e.g. if we call getFullMonth(1, 'Sunday') the function will generate the month of January with Jan 1st as a Sunday.

getFullMonth returns an array of objects where each object contains an integer 'dayNum' specifying the date and a string 'dayName' specifying the day of the week for that date. In our example getFullMonth(1, 'Sunday'), the output will be:

[{ dayNum: 1, dayName: 'Sunday' }, { dayNum: 2, dayName: 'Monday' }, { dayNum: 3, dayName: 'Tuesday' }, ..., { dayNum: 30, dayName: 'Monday' }, { dayNum: 31, dayName: 'Tuesday' }]

So using this getFullMonth function we can easily find if 13th of the month is Friday. We just need to call the function and in the output array check the 'dayName' property 13th element (index 12).

function monthHasFriday13th(month) {
    // if month is null, returns false
    return month && month[12].dayName === 'Friday'
}

const January = getFullMonth(1, 'Sunday');
console.log(monthHasFriday13th(January)); // true

const February= getFullMonth(2, 'Wednesday');
console.log(monthHasFriday13th(February)); // false

const invalidMonth = getFullMonth(16, 'Monday'); // returns null
console.log(monthHasFriday13th(invalidMonth)); // false

const invalidDay = getFullMonth(16, 'random'); // returns null
console.log(monthHasFriday13th(invalidMonth)); // false

MDN has really good documentation and tutorials, it would help you a lot to learn JavaScript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide

  • Related