mercredi 17 janvier 2018

What is wrong with my Random Test generator code for a new Kata for Code Wars? Receiving error message

I am working on my first Kata. I have a working Beta Kata and have received some good feedback. I am now trying to add some random tests.

I read some tutorial pages as to how to code the random tests. I thought I had successfully completed mine. I pasted it into the Test Cases window under my previous (nonrandom) test cases. I clicked the "Validate Solution" button. The code in the Complete Solutions window passed all the tests including the random tests. I validated it many times to generate a number of random tests. They all passed. But, when I clicked on the "Republish" link at the top of the Kata Beta page, I received this error:

“Initial Solution is invalid. (javascript) The code should not pass the kata test cases”

I have not changed the code in the Initial Solution window since I started working on the Kata. I did not get this error when I ran the first Test Cases before publishing. I did not get the message when I validated the random tests.

Here is the code in the Initial Solution window:

function birdCode(arr){


}

Here is most of the code for the random test generator. I did not include the function that solves the Kata.

My Kata gives the user an array of bird names/strings. The function they write will return an array of four letter codes in upper case. So, for the random tests, I set up an array of codes, and an object with codes as keys and names as values.

The code generates a random number from 0 to the length of the array of codes. It gets a code based on that number, and then gets the corresponding name from the object. It then pushes the code into one array (the correct code the user should return), and the name into another (the array which will be given to the user). The Test.Asserts then uses these two arrays.

function birdCode(arr){

// The function that solves the Kata is here.

return arrReturn;

}

///// Test Cases and Random Test Case Generation:

var arrTestCodes = ["BRAN", "ESCU", "YBMA", "BTTH", "CHUK", "COHA", "RNPH", "BCRF", "SURF", "BAOR", "YTWA", "RAGM", "LIMP", "MEGU", "VGSW", "LWFG"];

var codesNames = {
  BRAN:"Brant",
  CHUK:"Chukar",
  SURF:"Surfbird",
  LIMP:"Limpkin",
  ESCU:"Eskimo Curlew",
  COHA:"Cooper’s Hawk",
  BAOR:"Baltimore Oriole",
  MEGU:"Mew Gull",
  YBMA:"Yellow-Billed Magpie",
  RNPH:"Red-Necked Phalarope",
  YTWA:"Yellow-Throated Warbler",
  VGSW:"Violet-Green Swallow",
  BTTH:"Bare-throated Tiger-Heron",
  BCRF:"Brown-capped Rosy-Finch",
  RAGM:"Red-and-green Macaw",
  LWFG:"Lesser White-fronted Goose"
}

var nbr = 0;
var codeToUse = "";
var nameToUse = "";
var arrForRandomTest01 = [];
var arrForRandomResults01 = [];

for(var i = 0; i < 4; i++){
  nbr = Math.floor(Math.random() * (arrTestCodes.length - 1 + 1 - 0) + 0);
  codeToUse = arrTestCodes[nbr];
  nameToUse = codesNames[codeToUse];
  arrForRandomTest01.push(nameToUse);
  arrForRandomResults01.push(codeToUse);
}

Test.assertDeepEquals(birdCode(arrForRandomTest01),arrForRandomResults01)

I can easily modify this code to generate more than one random test case by creating more sets of arrays and creating more for loops that generate random numbers and populate the arrays. But, I would like to get this one random test working first.




Aucun commentaire:

Enregistrer un commentaire