5 kyu
multiply(value, times)
208wthit56
Description:
Here's something near-useless to fill your time! ;P
Your job is to write a function that will take any value
, and multiply it the specified number of times
. If the times
value is not valid, an error should be thrown.
Assuming the times
value is valid, the value
should be "multiplied" accoridng to the following criteria:
- Number: multiply
value
bytimes
. - String: create a string containing
value
multiple times. (eg."blah" * 3 == "blahblahblah"
.) Wheretimes
is 0, an empty string should be returned. - Function: create a function that will run the
value
function multiple times. Wheretimes
is 0, the new function should call thevalue
function 0 times. Each time the original function is called, the context and arguments should be preserved. - Object (non-null-like value): create an array containing
value
multiple times. (This is a shallow copy, using the original object, not cloning or anything.) Wheretimes
is 0, an empty array should be returned. - Anything else: should be returned as-is.
Here's a quick example of how things should work:
multiply(9, 3) == 9 * 3 == 27;
multiply("[string]", 2) == "[string]" + "[string]" == "[string][string]";
var i = 0;
var fn = multiply(function(a, b) {
if (this.is_context && (a === 1) && (b === "2")) {
console.log(i);
i++;
}
}, 3);
fn.call({ is_context: true }, 1, "2");
// LOGS:
// 0
// 1
// 2
var ref = {};
multiply(ref, 5) == [ref, ref, ref, ref, ref];
multiply(null, 10) == null;
NOTE: If
value
is a number,times
can be any non-infinite number. Ifvalue
is not a number,times
can be any integer >= 0. If these conditions are not met, an error should be thrown.
Strings
Fundamentals
Similar Kata:
Stats:
Created | Dec 30, 2014 |
Published | Dec 30, 2014 |
Warriors Trained | 1713 |
Total Skips | 154 |
Total Code Submissions | 8943 |
Total Times Completed | 208 |
JavaScript Completions | 208 |
Total Stars | 57 |
% of votes with a positive feedback rating | 81% of 53 |
Total "Very Satisfied" Votes | 40 |
Total "Somewhat Satisfied" Votes | 6 |
Total "Not Satisfied" Votes | 7 |
Total Rank Assessments | 9 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 6 kyu |