Valid Day Solution – Web Development
Articles,  Blog

Valid Day Solution – Web Development


Okay, so here we are. Here’s my version of the solution. We take in day from the user. First thing we need to do is we need to check if day is a number or not. Remember everything we get from–we get in our web app is going to be strings. HTTP and our web app doesn’t know whether the user is typing in numbers or strings or whatever, so we get all strings, and we have to make sure our string is made up of digits. That’s what the isdigit function does before we convert it to an integer, which is what the int type does here when we call it as a function. So now day, here, will be an integer, and if it’s between 0 and– if it’s greater than 0 and less than or equal to 31, we return it. You can see down here, I’ve run it on 25, and we got 25. Let’s try some of the error cases. I’ll try 0. We’ll run that. Our function returns none, which is what we expected. If I type in ‘foo’, one of our test strings–we’ll run that. We get none again. Let’s try a correct date again–31. We’ll run that. Okay, all is well. Now, we could have made this more complicated by also taking the month, and then making sure that February has only 28 days and March has 31 and that sort of thing, but we don’t need to make this too complicated. I’m not trying to quiz you on calendars. I’m just trying to quiz you on writing basic functions.

Leave a Reply

Your email address will not be published. Required fields are marked *