0

I have 3 variables that I'm doing tests on, I'm new in javascript, and I'm working on transforming the if/else statements to a switch for better code. But I need some guidance on how to do so given that I have 3 var day,month,year. Any help would be much appreciated. if((day!='' || month!='' || year!='')){

    if(day!='' && month =='' && year ==''){
        var day = d.setDate(d.getDate()+ parseInt(day));
    }else if(day!='' && month!='' && year ==''){
        var month =d.setDate(d.getMonth()+1+ parseInt(month));
        var day = d.setDate(d.getDate()+ parseInt(day));
    }else if(day!='' && month!='' && year!=''){
        var year =d.setDate(d.getFullYear()+ parseInt(year));
        var month =d.setDate(d.getMonth()+1+ parseInt(month));
        var day = d.setDate(d.getDate()+ parseInt(day));
    }else if(day!='' && year!='' && month ==''){
        var year =d.setDate(d.getFullYear()+ parseInt(year));
        var day = d.setDate(d.getDate()+ parseInt(day));
    }else if(month!='' && year!='' && day ==''){
        var year =d.setDate(d.getFullYear()+ parseInt(year));
        var month =d.setDate(d.getMonth()+1+ parseInt(month));
    }else if(month!='' && day =='' && year ==''){
        var month =d.setDate(d.getMonth()+1+ parseInt(month));
    }else if(year!='' && day =='' && month ==''){
        var year =d.setDate(d.getFullYear()+ parseInt(year));
    }

}
JDOE
  • 25
  • 6
  • Possible duplicate of [Switch statement multiple cases in JavaScript](http://stackoverflow.com/questions/13207927/switch-statement-multiple-cases-in-javascript) – Muhammad Saqlain Jan 22 '17 at 09:40
  • You will never enter the second condition. If both day and month are not ' ' then you are still trapped in the first if where day is not ' '. – gautam1168 Jan 22 '17 at 09:43
  • You'd better not repeat parseint, use it once. – shukshin.ivan Jan 22 '17 at 09:43
  • The code does not make sense. Why would you want to call `setDate` with a month value or year value as argument? Can you explain what you want to achieve? What is `d`? And what is intended it should be after your code runs? – trincot Jan 22 '17 at 09:49
  • @trincot i have a field with a date (d) and 3 other fields, day month year..which i'm trying to add to (d) – JDOE Jan 22 '17 at 09:52
  • @trincot i got it to work with adding days, and adapting to date restrictions.. but i'm failing when adding month and year – JDOE Jan 22 '17 at 09:54

2 Answers2

0

Welcome! What are you trying to achieve? Currently, your logic is broken. For example, if "day" isn't empty, the first "if" clause will fire, but none of the others that test for "day" being empty...

Simon Catlin
  • 2,141
  • 1
  • 13
  • 15
0

Some issues and remarks:

  • You should treat the modifications separately, as you gain nothing from testing each combination. The effect of year, month and day adjustments can be applied independently.

  • You are calling the wrong method for adjusting the month and/or year

  • These methods return a timestamp, so you should not assign the result to your year, month or day variables. The d variable will be mutated by the call, which is apparently all you need.

  • Conversion to integer can be done with the unitary +

  • Although the month numbering is zero based, it makes no sense to add 1 extra, when the day variable is supposed to add a number of months. So drop the + 1.

So you can simplify as follows:

if (year !== '') d.setFullYear(d.getFullYear() + +year);
if (month !== '') d.setMonth(d.getMonth() + +month);
if (day !== '') d.setDate(d.getDate() + +day);
trincot
  • 317,000
  • 35
  • 244
  • 286
  • dear sir, you are the greatest person in my life today!! Thank you very much for your time and insight on all the points you marked. – JDOE Jan 22 '17 at 10:05