The task was based on the date entered by a user to determine:
- Whether the date is valid(proper format, plus the month number to not exceed 12, and the date number not to exceed the number of days that each single month have)
- What day of the week that specific day was(or will be)
- Which day in row of that specific year that day was( or will be)
- Whether that specific year is leap or simple year
- Which specific day of the week in row of that specific year and month the day enter was(or will be)?!( Example date 2020-12-31, 31 of December 2020 was Thursday and it was 53 Thursday of the year 2020, and 5 th Thursday of the month of December 2020)
I resolved the task successfully, but I'm still excessively using control flow and if statements which it seems to me consume me a lot of lines of codes! Could all of this be accomplished by fewer lines of code and without excessive using of control flow? When is unavoidable to use control flow and when it could be avoided? Here is my code:
var flag = false;
while(!flag){
var anydate = prompt('Enter the date!', anydate);
let parts = anydate.split('-');
yyyy = Number(parts[0]);
mm = Number(parts[1]);
dd = Number(parts[2]);
if ((yyyy<0)||(mm<1)||(mm>12)||(dd>31)||(typeof(anydate)==='undefined')||(yyyy%1!==0)|| (mm%1!==0)||(dd%1!==0)||((mm===2)&&(dd>29))||((mm===4)&&(dd>30))||((mm===6)&&(dd>30))||((mm===9)&&(dd>30))||((mm===11)&&(dd>30))){
alert('Your date is invalid! Please enter valid date!');
continue;
flag = false;
}else{
alert('OK! You can continue with process!');
break;
};
}
if (yyyy%4===0){
alert('This is leap year! The month of February has 29 days!');
}else{
alert('This is simple year! The month of February has 28 days!');
}
anydate = new Date(anydate);
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
dayoftheweek = weekday[anydate.getDay()]
d = anydate.getDate();
m = anydate.getMonth();
console.log(weekday[anydate.getDay()]);
console.log(anydate.getMonth()+1);
console.log(anydate.getDate());
console.log(anydate.getFullYear());
console.log(anydate);
var start = new Date(anydate.getFullYear(), 0, 0);
var diff = (anydate - start) + ((start.getTimezoneOffset() - anydate.getTimezoneOffset()) * 60 * 1000);
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
alert('This is the day ' + day + ' of the year!');
var q = Math.floor(day/7)+1;
q = Number(q);
var q1 = Math.floor(d/7)+1;
q1 = Number(q1);
if(dayoftheweek==='Friday'){
alert('This is ' + q + ' Friday of the year and ' + q1 + ' Friday of the month ' + mm);
}else if(dayoftheweek==='Saturday'){
alert('This is ' + q + ' Saturday of the year and '+ q1 + ' Saturday of the month '+ mm);
}else if(dayoftheweek==='Sunday'){
alert('This is ' + q + ' Sunday of the year and '+ q1 + ' Sunday of the month ' + mm);
}else if(dayoftheweek==='Monday'){
alert('This is ' + q + ' Monday of the year and '+ q1 + ' Monday of the month ' + mm);
}else if(dayoftheweek==='Tuesday'){
alert('This is ' + q + ' Tuesday of the year and '+ q1 + ' Tuesday of the month ' + mm);
}else if(dayoftheweek==='Wednesday'){
alert('This is ' + q + ' Wednesday of the year and '+ q1 + ' Wednesday of the month ' + mm);
}else if(dayoftheweek==='Thursday'){
alert('This is ' + q + ' Thursday of the year and '+ q1 + ' Thursday of the month ' + mm);
}else{
alert('Done!');
}
console.log(day);