0

The task was based on the date entered by a user to determine:

  1. 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)
  2. What day of the week that specific day was(or will be)
  3. Which day in row of that specific year that day was( or will be)
  4. Whether that specific year is leap or simple year
  5. 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);
RobG
  • 142,382
  • 31
  • 172
  • 209
Ivan Vrzogic
  • 157
  • 1
  • 8
  • why do you convert numbers to number? – Nina Scholz Jan 02 '21 at 10:49
  • Good question! To be sure that parts of date as date, month and year are not strings.... – Ivan Vrzogic Jan 02 '21 at 10:52
  • At which point is `flag` supposed to get assigned `true`? Why using `flag` at all if one breaks/leaves the loop by a criteria which is different from the boolean value of `flag`? – Peter Seliger Jan 02 '21 at 12:12
  • While loop is created for the purpose of returning back to the beginning when date entered is of improper format! Otherwise it wouldn't work, or it wouldn't get out of if statement and return to the beginning if date entered is of improper format! Boolean variable flag is created for the purpose of creating condition for while loop to exist! Beginning value of flag = false, and the while loop has beginning value of flag = true (while(!flag))! – Ivan Vrzogic Jan 02 '21 at 13:25
  • See [*How to validate a date?*](https://stackoverflow.com/questions/5812220/how-to-validate-a-date?r=SearchResults&s=2|160.2945). The result could be achieved in way less code. The OP contains errors (e.g. `typeof(anydate)==='undefined'` should be `typeof anydate === undefined`, but *prompt* always returns type string, `yyyy%4===0` is not a sufficient test for leap years, e.g. 1900 was not a leap year, or will 2100 be a leap year, many undeclared variables, redundant *continue* statement…). But this is not the site you should use for reviewing code. – RobG Jan 02 '21 at 13:55
  • I have to admit that I didn't know that about leap years, that 1700, 1800, and 1900 are not leap years, but I've corrected all of that and entered those additional conditions. typeof (anydate) === 'undefined' is working ok in my code! – Ivan Vrzogic Jan 02 '21 at 15:43
  • @RobG, `typeof` returns a string. – Nina Scholz Jan 02 '21 at 17:56
  • @NinaScholz—yes, it was late… ;-) – RobG Jan 02 '21 at 20:26

0 Answers0