+ 1

Why are months, weeks, and days equal to NaN?

Can't figure out why months, weeks, and days are NaN, but years works perfectly fine. Suggestions? https://code.sololearn.com/WKGYr24kOCDH/?ref=app

28th Feb 2018, 8:46 PM
jacksonofgames 28
jacksonofgames 28 - avatar
26 Respuestas
+ 6
Because you are using variables that haven't as yet gotten calculated in your expressions. For example, line 29 year isn't prompted for as yet, but you use it here. Reorganize to prompt for everything first and then check for valid. Finally, process how many of things you are. If you use mm/dd/yyyy format with single prompt, use split on the / to make 3 strings. Verify in year, month, and day order.
28th Feb 2018, 9:39 PM
John Wells
John Wells - avatar
+ 5
Made a couple of minor changes to fix forgotten offset 1900 and 1899 lines.
1st Mar 2018, 4:00 AM
John Wells
John Wells - avatar
+ 4
Line 40 gets a 0 because days isn't set until third runs. The numbers are not based on the date entered as anything I entered had the same result. In another hour or so, I'll be able to spend more time on this.
1st Mar 2018, 1:28 AM
John Wells
John Wells - avatar
+ 4
Line 194 to 196 are wrong: resultm = ((years - (year - 1) * 12) + month); resultw = ((years - (year - 1) * 52) + weeks); resultd = (years - ((year - 1) * 365.25) + days ); (2017-1)*12 subtracted from 2018 gives negative number. (years-year-1)*12 gets you closer. month isn't the right thing to be subtracting. Enter 2/27/18 you should get age=0, month=0, weeks=0, days=1 Enter 2/28/17 you should get age=1, month=12, weeks=52, days=365.25
1st Mar 2018, 2:55 AM
John Wells
John Wells - avatar
+ 4
var today = new Date(); var datestring = ""+month+"/"+day+"/"; if (year == today.getYear()+1900 || month < today.getMonth() || (month == today.getMonth() && day <= today.getDay())) datestring = datestring+year; else datestring = datestring+(today.getYear()+1899); var lastBD = new Date(datestring); var days = Math.floor((today.getTime()-lastBD.getTime())/(1000*3600*24)); This should get days to last birthday, which can be used to add your offsets.
1st Mar 2018, 3:49 AM
John Wells
John Wells - avatar
+ 3
ceil should be floor so today is zero not 1.
1st Mar 2018, 4:02 AM
John Wells
John Wells - avatar
+ 3
My code is off, if birthday day is same as today by a month. I'll fix it soon. I've got things I must do for next few hours. Your code should be able to work, but I wanted the right answers to compare against. Having coded it, I figured I share.
2nd Mar 2018, 6:38 PM
John Wells
John Wells - avatar
+ 3
I renamed, formatted, and cleaned up things a bunch. https://code.sololearn.com/Ws8BoQ892Spr
3rd Mar 2018, 5:55 PM
John Wells
John Wells - avatar
+ 2
You're right I got confused. It has been a long day. Sorry...
1st Mar 2018, 3:24 AM
John Wells
John Wells - avatar
+ 2
Line 223 is missing + before day. Line 243 needs } to close third function off.
2nd Mar 2018, 3:11 AM
John Wells
John Wells - avatar
+ 2
Not that I'm suggesting you change your program, but I figured I'd share my version. It isn't perfect as the 365.25 doesn't work right when today is your birthday. https://code.sololearn.com/Wrthly1OH052 What inputs have you tried? I have tried with today, yesterday, and a year ago as the answers are easy to come up with. If I remove the -1 from lines 289 to 291, it gets close. Yesterday gives 0 same as today. A year ago is correct.
2nd Mar 2018, 5:09 PM
John Wells
John Wells - avatar
+ 2
I'm cleaning up the code from a few hours ago to be formatted and removing extra code so I can see what is going on. Have you changed your version recently?
3rd Mar 2018, 3:26 AM
John Wells
John Wells - avatar
+ 2
I will figure it out and make it right.
3rd Mar 2018, 3:39 AM
John Wells
John Wells - avatar
+ 1
Thank you
2nd Mar 2018, 6:43 PM
jacksonofgames 28
jacksonofgames 28 - avatar
+ 1
Okay thank you
3rd Mar 2018, 4:02 AM
jacksonofgames 28
jacksonofgames 28 - avatar
+ 1
I don't understand that instead of putting an else if for each month, why don't you just group 31 days months and 30 days months and put 'or' in if statement?
3rd Mar 2018, 2:50 PM
Rugved Modak
Rugved Modak - avatar
0
Okay I've reorganized the calculations so that they execute after all the variables used in them are defined, but it gives me negative numbers
1st Mar 2018, 12:28 AM
jacksonofgames 28
jacksonofgames 28 - avatar
0
You mean the prompt variables are not based on the date entered?
1st Mar 2018, 1:31 AM
jacksonofgames 28
jacksonofgames 28 - avatar
0
Good eye
2nd Mar 2018, 3:59 AM
jacksonofgames 28
jacksonofgames 28 - avatar
0
That code is spot on! On my code, if the month they enter is greater than the current month (+ 1 because it is zero based), then my months are correct, and my days are off by 30 or 31. If the month they enter is less than the current month + 1, my month is off by 1, and my days are off by roughly 350
2nd Mar 2018, 6:27 PM
jacksonofgames 28
jacksonofgames 28 - avatar