Skip to content

Instantly share code, notes, and snippets.

@muhsalaa
Created September 19, 2020 06:25
Show Gist options
  • Save muhsalaa/31cb81ce67d60ea5d223dbc5b1b0100e to your computer and use it in GitHub Desktop.
Save muhsalaa/31cb81ce67d60ea5d223dbc5b1b0100e to your computer and use it in GitHub Desktop.
function to get number need to be highlighted.
// question at https://muhsalaa.com/journal/3ab005b6-2c66-4ccc-b7bb-df1be5ff8777
function highlight(nominal, uniqueCode) {
// convert number to string
const totalString = String(nominal + uniqueCode);
const nominalString = String(nominal);
let count = 0;
// loop to count similar character by comparing nominal and total (nominal+unique code)
for (let x = 0; x < totalString.length; x++) {
if (nominalString[x] === totalString[x]) {
count += 1;
} else {
// once it found different character, loop will break
break;
}
}
// finally, slice total amount with count, or number of same character
// we will get the affected number by unique code addition
return totalString.slice(count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment