5 kyu

String insert values

896 of 903ankr

Description:

Given a string and an object/array you need to return a formatted string. Replace all occurrences in the string where the name of a key in the object is surrounded by curly brackets.

Inherited object properties should not be applied

An example says more than a thousand words..

Example using object

var s = 'Hello {foo} - make me a {bar}';
var o = {
  foo : 'Jack',
  bar : 'sandwich'
};

format(s, o); // "Hello Jack - make me a sandwich"

Example using array

var s = 'Hello {0} - make me a {1}';
var a = ['Jack', 'sandwich'];

format(s, a); // "Hello Jack - make me a sandwich"

See tests for more information.

Strings
Algorithms

Stats:

CreatedDec 1, 2013
PublishedDec 1, 2013
Warriors Trained1907
Total Skips72
Total Code Submissions5004
Total Times Completed903
JavaScript Completions896
Total Stars34
% of votes with a positive feedback rating80% of 133
Total "Very Satisfied" Votes94
Total "Somewhat Satisfied" Votes24
Total "Not Satisfied" Votes8
Ad
Contributors
  • ankr Avatar
  • jhoffner Avatar
Ad