Checks input of factorial#220
Conversation
Makes sure that the "n" variable input is valid by checking if it is greater than or equal to zero and if it is indeed a whole number. If it is not, the program prints that "n" is an invalid input.
| void main() { | ||
| var n = 5; | ||
| var fac = factorial(n); | ||
| if(n>=0 && n.runtimeType == int){ |
There was a problem hiding this comment.
Instead of checking types, Why not replace var n =5; with int n=5;
This will help us avoid the unnecessary type checking.
There was a problem hiding this comment.
We can do that, I would just say to leave it a variable in case if user input is needed. Whatever you want though.
There was a problem hiding this comment.
Since we are not taking any user input and dart has a great types. let's exploit that.
you can add test for the same.
| var fac = factorial(n); | ||
| print("$n! = $fac"); /* output: 5! = 120 */ | ||
| }else{ | ||
| print("Invalid input. Input has to be a positive whole integer"); | ||
| } |
There was a problem hiding this comment.
can you add more tests to this file. Checkout other files to see how tests are written there.
Two sum File
There was a problem hiding this comment.
Let's remove these print statements and have proper tests for these functions. im in the process of upgrading the dart version and adding tests to old files so we can migrate to null safety and know all algorithms are working fine with Null safety.
There was a problem hiding this comment.
You want me to put more tests in there for the factorials? Are they the same as the ones that you put on the two sum file?
There was a problem hiding this comment.
Currently we are just having print statements. Instead of prints. Let's have tests which are more effective. I have shared the file for you reference. you can check some other files in the repo as well to get better clarity on writing tests.
There was a problem hiding this comment.
I can't think of any tests to add, sorry...let me know if there are specific tests I can add
This change simply just adds a feature to the factorial part to make sure that the input is valid. Specifically, it makes sure that the "n" variable input is valid by checking if it is greater than or equal to zero and if it is indeed a whole number. If it is not, the program prints that "n" is an invalid input. I agree to all of the terms below.
Welcome to Dart community
Describe your change:
Checklist:
Fixes: #{$ISSUE_NO}.