Skip to content

Instantly share code, notes, and snippets.

@AngeloAnolin
Created February 26, 2024 20:53
Show Gist options
  • Save AngeloAnolin/1b97a60d6fa3472965a2b13029445d13 to your computer and use it in GitHub Desktop.
Save AngeloAnolin/1b97a60d6fa3472965a2b13029445d13 to your computer and use it in GitHub Desktop.
Given a number and a digit to remove from that number, maximize the resulting number after the digit has been removed and print it.
/*
From the challenge provided, it is evident that the right-most number should be removed to get the maximum value when the digit is removed.
*/
const removeDigit = (val, digit) => {
const length = digit.toString().length
if (length > 1) {
return console.log(`You can only pass a single digit`)
}
const str = val.toString()
const lastIndex = str.lastIndexOf(digit)
if (lastIndex !== -1) {
return console.log(`${str.slice(0, lastIndex)}${str.slice(lastIndex + 1)}`)
}
console.log(`Digit '${digit}' not found. Return original value of ${val}`)
}
// Testing
removeDigit(23456, 3)
removeDigit(83388833333, 8)
removeDigit(123, 4)
removeDigit(246, 'S')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment