Skip to content

Instantly share code, notes, and snippets.

@jelera
Last active August 2, 2019 18:02
Show Gist options
  • Save jelera/46c02aa9b40638c5d46c7c1983cb4339 to your computer and use it in GitHub Desktop.
Save jelera/46c02aa9b40638c5d46c7c1983cb4339 to your computer and use it in GitHub Desktop.
Code Challenge to pad zeroes to integers inside a string
//
//****************************************************************************//
//
// HOW TO RUN IT
// ------------------------------
// Open the command line and using the node.js interpreter
// $ node codechallenge.js
//
//
// SOLUTION TO THE CODE CHALLENGE
// ------------------------------
//
// 1. Split the string using a regular expression to match a group of numbers
//
// 2. Mapping over every single element of the splitted array
//
// 3. evaluate the array and operate in every item has a value of integers
//
// 4. We would only add the zeroes if the number of characters is less than the
// amount of zeroes to add
//
// 5. I found two solutions, one that uses a built-in String method `padStart`
// and another that uses a `for` loop and prepends the zeroes to the beginning
// of the string
//
// 6. Once all the parts of the splitted array had been processed, we proceed
// to join them back again
//
// ADDITIONAL INFO
// ---------------
//
// I'm using JavaScript Standard Style guide, so the semicolons can be ommitted
//
//****************************************************************************//
//
function padIntegerFromString(string, numCharacter) {
// We can change the character from 0 to whatever we want
const character = '0'
// 1. Split the string using a regular expression to match a group of numbers
let splittedArray = string.split(/(\d+)/)
// 2. Mapping over every single element of the splitted array
let processedArray = splittedArray.map(item => {
// 3. evaluate the array and operate in every item has a value of integers
if (parseInt(item, 10)) {
const charLength = item.length
// 4. We would only add the zeroes if the number of characters is less
// than the amount of zeroes to add
if (charLength < numCharacter) {
// 5. I found two solutions, one that uses a built-in String method
// `padStart` and another that uses a `for` loop and prepends the
// zeroes to the beginning of the string
//
// --------------------------------------------//
// => SOLUTION 1
// --------------------------------------------//
// Use the built-in String method `padStart`
//
// By commenting out the following line, and commenting in the
// other solution
// item = item.padStart(numCharacter, character)
// --------------------------------------------//
// => SOLUTION 2
// --------------------------------------------//
// Use a simple For loop and string concatenation
const zeroesToAdd = numCharacter - charLength
for (let i = 0; i < zeroesToAdd; i++) {
item = character + item
}
return item
} else {
return item
}
} else {
return item
}
})
// 6. Once all the parts of the splitted array had been processed, we proceed
// to join them back again
return processedArray.join('')
}
//****************************************************************************//
//
// EXAMPLES
//
//****************************************************************************//
const examples = [
['area 59', 4],
['123 Foo st', 5],
['123 Foo st', 2],
['Area59asdf234', 4]
]
examples.forEach(example => {
const exampleString = example[0]
const numChar = example[1]
console.log(padIntegerFromString(exampleString, numChar))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment