1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-10-29 22:17:23 +01:00

feat(solutions): add left-pad/javascript/function

This commit is contained in:
Divlo 2023-05-21 12:02:22 +02:00
parent 47de82f417
commit f9a5b15e7a
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
3 changed files with 46 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# left-pad/javascript/function
Created by [@Divlo](https://github.com/Divlo) on 21 May 2023.

View File

@ -0,0 +1,3 @@
{
"type": "module"
}

View File

@ -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)