-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCC-10.js
26 lines (22 loc) · 1.19 KB
/
DCC-10.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Prompt:
// - Write a function called formatWithPadding that accepts three arguments:
// - A numeric argument (an integer) representing the number to format.
// - A string argument (a single character) representing the character used to "pad" the returned string to a minimum length.
// - Another numeric argument (an integer) representing the length to "pad" the returned string to.
// - The function should return the integer as a string, "left padded" to the length of the 3rd arg using the character provided in the 2nd arg.
// - If the length of the integer converted to a string is equal or greater than the 3rd argument, no padding is needed - just return the integer as a string.
// Examples:
// formatWithPadding(123, '0', 5); //=> "00123"
// formatWithPadding(42, '*', 10); //=> "********42"
// formatWithPadding(1234, '*', 3); //=> "1234"
// -----------------------------------------------------------------*/
// // Your solution for 10-formatWithPadding here:
const formatWithPadding = (num, str, padLen) => {
const numStr = num.toString()
if(numStr.length >= padLen){
return numStr
} else{
const padding = str.repeat(padLen - numStr.length)
return padding + numStr
}
}