mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
41 lines
837 B
JavaScript
41 lines
837 B
JavaScript
|
import readline from 'node:readline'
|
||
|
|
||
|
/**
|
||
|
*
|
||
|
* @param {string} string
|
||
|
* @param {number} resultLength
|
||
|
* @param {string} padString
|
||
|
* @returns {string}
|
||
|
*/
|
||
|
const leftPad = (string, resultLength, padString) => {
|
||
|
resultLength = resultLength - string.length
|
||
|
if (resultLength <= 0) {
|
||
|
return string
|
||
|
}
|
||
|
let pad = ''
|
||
|
while (resultLength !== 0) {
|
||
|
if (resultLength & 1) {
|
||
|
pad += padString
|
||
|
}
|
||
|
resultLength >>= 1
|
||
|
if (resultLength) {
|
||
|
padString += padString
|
||
|
}
|
||
|
}
|
||
|
return pad + string
|
||
|
}
|
||
|
|
||
|
const solution = () => {
|
||
|
console.log(leftPad(input[0], Number(input[1]), input[2]))
|
||
|
}
|
||
|
|
||
|
const input = []
|
||
|
const readlineInterface = readline.createInterface({
|
||
|
input: process.stdin,
|
||
|
output: process.stdout
|
||
|
})
|
||
|
readlineInterface.on('line', (value) => {
|
||
|
input.push(value)
|
||
|
})
|
||
|
readlineInterface.on('close', solution)
|