5 kyu

Handy Toolbox for Kata authors

Description:

Handy Toolbox for Kata authors

Hello there, code warrior! I'm currently planning to author an awesome and challenging Kata in the near future (not guaranteed ;) ) but I need your help. Can you help me?

Mission Brief

Overview

Your mission is to define a KataToolbox class which contains handy built-in class methods that a Kata author can use alongside the existing Codewars Test framework in order to make authoring top-quality Kata effortless.

Details

I. stripwhitespace

Define the class method KataToolbox.stripwhitespace, which effectively removes all whitespace and newline characters from a given string. For example:

KataToolbox.stripwhitespace(" s t r i n g ") === "string"
KataToolbox.stripwhitespace("Hello World") === "HelloWorld"
KataToolbox.stripwhitespace("Lorem ipsum dolor sit amet, adispicing eu.") === "Loremipsumdolorsitamet,adispicingeu."
KataToolbox.stripwhitespace(`Lorem ipsum dolor sit amet, adispicing eu.

Lorem ipsum dolor sit amet, adispicing eu.

Lorem ipsum dolor sit amet, adispicing eu.`) === `Loremipsumdolorsitamet,adispicingeu.Loremipsumdolorsitamet,adispicingeu.Loremipsumdolorsitamet,adispicingeu.`

Your method should not alter the original value passed in.

II. approxEquals

Usually, when comparing numerical values in a test, we want the user's algorithm to return exactly the same value as the established solution (as defined by the Kata author). For example, when testing the user's function for returning the 5th prime number, we would want their function to return exactly 11, not something like 10.999 or 11.001, because if their function/algorithm returned anything different, it would mean that their algorithm is somewhat faulty.

However, as we all know, floating point arithmetic in just about any programming language is not commutative; that is, you may get a slightly different answer depending on the order in which you carry out the calculations. For example, if you do something like (10 / 3) * 3 you would expect 10 but in reality you may get something like 9.9999999999 instead of 10 due to rounding errors. For this reason we want to implement a method KataToolbox.approxEquals which returns true if 2 numbers round to the same value to 3 decimal places, false otherwise. Your method should also throw an error if either of the arguments passed in is not a number.

KataToolbox.approxEquals(6, 6) === true
KataToolbox.approxEquals(3.14, 3.15) === false
KataToolbox.approxEquals(3.141, 3.142) === false
KataToolbox.approxEquals(3.1415, 3.1424) === true // Both round to 3.142 to 3 decimal places
KataToolbox.approxEquals(Math.PI, "hello world") // throws an error

III. saveMathRandom

NOTE: This is NOT a class method. A new instance of the toolbox will be created before this method is called.

The saveMathRandom method should generate a random float from 0 (inclusive) to 1 (exclusive), just like Math.random (look out, an obvious hint here ;) ). The only catch here is that your saveMathRandom method must continue to generate random floats properly even if a cheater successfully disables the original Math.random.

IV. randomString

The class method KataToolbox.randomString should generate a random string, 10 characters in length, that must contain at least alphanumeric characters (both uppercase and lowercase) plus any other characters you may wish to add. For example:

KataToolbox.randomString() === "aE4i5Ker7N" // Must contain at least alphanumeric characters
KataToolbox.randomString() === "Zb$jY%nOm5" // Random string may also contain any other type of character of your choice

NOTE: Due to the random nature of this class method, your method may occassionally fail one or two tests. If this happens, simply submit your solution again until it passes all the tests.

V. userCheated

Well, it's sometimes quite hard to tell if a user has cheated in your Kata; hell, sometimes you aren't even sure how to define "cheating"! But for the purposes of this Kata, a user is clearly cheating if he or she redefines Math.random in such a way that it returns the same number every time. So for this class method (KataToolbox.userCheated), you should check whether the user has cheated or not using this logic. Return true if the user has cheated; false otherwise.

Good luck :D

Fundamentals

Stats:

CreatedMar 21, 2016
PublishedMar 22, 2016
Warriors Trained147
Total Skips3
Total Code Submissions812
Total Times Completed49
JavaScript Completions49
Total Stars10
% of votes with a positive feedback rating90% of 25
Total "Very Satisfied" Votes22
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes2
Total Rank Assessments5
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • donaldsebleung Avatar
  • Voile Avatar
Ad