Skip to content

Instantly share code, notes, and snippets.

@connor11528
Created March 17, 2021 22:43
Show Gist options
  • Save connor11528/e6874958e3394b2a3657e37ef99b76f1 to your computer and use it in GitHub Desktop.
Save connor11528/e6874958e3394b2a3657e37ef99b76f1 to your computer and use it in GitHub Desktop.
Sample problem to upgrade software version
/*
We want to upgrade from initialVersion to targetVersion. To do this
we run through these steps:
- First, we need one upgrade for each incremental major version
(resetting the other numbers to 0)
- Next, we need one upgrade for each incremental minor version
(resetting the third and fourth numbers, if applicable, to 0)
- Then, we need a one-time upgrade to the target third number
(if applicable)
- Finally, we need a one-time upgrade to the target fourth number
(if applicable)
Write a function updateVersion that returns an array of strings
containing all the intermediate versions, including the initial
version and the target version, in order of upgrade.
Examples:
- To upgrade a merchant from 2.1 to 3.2, we need to do:
2.1 → 3.0 → 3.1 → 3.2
- To upgrade a merchant from 3.3.2 to 4.2.7, we need to do:
3.3.2 → 4.0.0 → 4.1.0 → 4.2.0 → 4.2.7
- To upgrade a merchant from 4.1.3.56 to 6.2.3.170,
we need to upgrade as follows:
4.1.3.56 → 5.0.0.0 → 6.0.0.0 → 6.1.0.0 → 6.2.0.0 → 6.2.3.0 → 6.2.3.170
*/
// Pseudocode
// - result = []
// - add input to result (result = ["2.1"])
// - split the input into an array
// - up the major version to target major version
// - if array[0] != finalVersion[0] -> array[0]+=1
// - while the major version is less than or equal to target, increment it and add with zeros to list
// - update minor version with same process
// - update subminor version with same process
// - update subsubminor version with same process
console.log(upgradeVersion('4.1.3.56', '6.2.3.170'));
function upgradeVersion(initialVersion, targetVersion) {
let output = [initialVersion];
let currVersion = "";
const iv = initialVersion.split('.');
const tv = targetVersion.split('.');
const versionLength = iv.length;
for (let i = 0; i < versionLength; i++){
if (i > 1) {
iv[i] = tv[i];
const versionString = iv.fill(0, i + 1).join('.');
output.push(versionString);
}
while(iv[i] < tv[i]) {
iv[i] = parseInt(iv[i]) + 1;
const versionString = iv.fill(0, i + 1).join('.')
output.push(versionString);
}
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment