Beta

RuplesJS #4: String Formatting

Description:

The RuplesJS team has now added you in the credits as a contributor to the project, thanks to all the fine work you've done. They're hearing a lot of positive feedback from the Ruby community that this library is really helping them transition over to JavaScript easier. After your team meeting, you've come away with a high priority task: string formatting!

##String.sprintf()

We're going to create a prototype method in JavaScript called sprintf() that will be similar in a way to Ruby's sprintf() function. In fact, many languages have string formatting via sprintf() or format(). Our method should accept any number of arguments, and return a new string. Example:

"This is RuplesJS number %d".sprintf(4) -> "This is RuplesJS number 4"
"%d,%d,%d GO!".sprintf(1,2,3) -> "1,2,3 GO!"
"Hello, %s! My name is %s.".sprintf("CodeWars", "Nate") -> "Hello, CodeWars! My name is Nate."

Those were some pretty simple examples. Since we won't be copying Ruby's string formatting exactly, let's add some of our own stuff too. See the chart below:

Format Code      Description
%d               Print a number
%s               Print a string
%c               Print a character. If a string is given, only prints the first character.
                 "My initials are %c.%c.".sprintf("Nate","Brady") -> "My initials are N.B."
%b               Prints true of false from a boolean
                 "It is $b!".sprintf(1==1) -> "It is true!"
%2f              Print a number to n decimal places.
                 Ex. "%3f".sprintf(20) -> 20.000
                     "%1f".sprintf(3.1415) -> 3.1 (Does not round)

####Other: A number may be inserted between % and the letter code (except % and f) to indicate the number of times that particular argument should be displayed. Ex. "%5d%2d%d".sprintf(1,0,1) -> "11111001"

Arguments may come in any order, but there must be a proper number of arguments to match the codes given. If there are more arguments than codes, more codes than arguments, or they don't have the proper number of matches, throw an error. Strings arguments may only be used for %s & %c, while number arguments may only be used for %d and %(n)f. 0, 1, true or false may only be used for %b. % signs that appear but that are not part of a proper format code should be displayed as they appear.

More complex examples:

"%d: %s is out of order.".sprintf("This",1) -> "1: This is out of order."
"%c here and %s here".sprintf("One","O") -> "O here and O here."
"%d,%d,%s,%s".sprintf(1,2,3,"GO!") -> Throws Error
"%d - %1f - %s - %c".sprintf("A","B",1,2)->"1 - 2.0 - A - B"
"%d: Does %t do anything?".sprintf(2) -> "2: Does %t do anything?"
// Boolean codes (%b) will consume 0's and 1's and may cause errors like:
"%b %d".sprintf(0, false) -> Throws Error since 0 is consumed by %b and false then has no code
// This is because %b looks for the first available match. 0, 1, true and false are all matches for %b

I've provided most of the test suite, excluding the random tests, in case of confusion about what's expected. Good luck and Happy coding!

Check out my other RuplesJS katas:
Fundamentals

Stats:

CreatedJan 10, 2016
PublishedJan 10, 2016
Warriors Trained198
Total Skips50
Total Code Submissions221
Total Times Completed29
JavaScript Completions29
Total Stars6
% of votes with a positive feedback rating69% of 13
Total "Very Satisfied" Votes9
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes4
Total Rank Assessments10
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • NateBrady23 Avatar
  • B1ts Avatar
Ad