mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add left-pad/javascript/function
This commit is contained in:
parent
47de82f417
commit
f9a5b15e7a
@ -0,0 +1,3 @@
|
|||||||
|
# left-pad/javascript/function
|
||||||
|
|
||||||
|
Created by [@Divlo](https://github.com/Divlo) on 21 May 2023.
|
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"type": "module"
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
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)
|
Loading…
Reference in New Issue
Block a user