I have function in codewars.com getNumberLength(n)
which gets some number in different digits and should give you the length of number. For exampl number n=2000
shoul give 4.
I also added some test cases with Test.assertEquals()
method, but now I need to add test case with random numbers, does somebody know how to do that in codewars, because in their documentation I could not find explanation for random test case. They have their own test case methods, guys who have experience help please? For information function written in javascript.
Asked
Active
Viewed 1,916 times
2

Bereket
- 31
- 3
-
1Sharing your research helps everyone. Tell us what you've tried and why it didn't meet your needs. This demonstrates that you've taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! Also see [how to ask](http://stackoverflow.com/questions/how-to-ask) – Cerbrus Sep 14 '15 at 05:51
-
Generate random numbers, and add a embedded sample solution in the test file, to run test user's solution. – Eric Mar 13 '21 at 07:43
2 Answers
2
I suggest using a function to generate random numbers like what is discussed here. Then you can doing something like:
function random(min,max) {
return Math.floor(Math.random()*(max-min+1)+min);
};
Test.assertEquals(getNumberLength(random(1, 100)), 3)
Test.assertEquals(getNumberLength(random(1, 1000)), 4)
Test.assertEquals(getNumberLength(random(1, 10000)), 5)
Test.assertEquals(getNumberLength(random(1, 100000)), 6)

Community
- 1
- 1

Adam Recvlohe
- 334
- 1
- 11
1
The test for my Kata with random Numbers look like this:
Test.describe("Random Tests:", function() {
function solution(d) {
if (d<3) return d*40;
return (d<7) ? d*40-20 : d*40-50;
}
for (let i = 0; i < 100; i++) {
let days = ~~(Math.random() * 100) + 1;
Test.assertEquals(rentalCarCost(days), solution(days));
}
});
I hope this is a good example and helps you.