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 () }
Advertisement
Answer
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