-
Notifications
You must be signed in to change notification settings - Fork 0
/
6kyu-next-version.js
31 lines (23 loc) · 1.09 KB
/
6kyu-next-version.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// You're fed up about changing the version of your software manually. Instead, you will create a little script that will make it for you.
// Exercice
// Create a function nextVersion, that will take a string in parameter, and will return a string containing the next version number.
// For example:
// nextVersion("1.2.3") === "1.2.4";
// nextVersion("0.9.9") === "1.0.0";
// nextVersion("1") === "2";
// nextVersion("1.2.3.4.5.6.7.8") === "1.2.3.4.5.6.7.9";
// nextVersion("9.9") === "10.0";
// Rules
// All numbers, except the first one, must be lower than 10: if there are, you have to set them to 0 and increment the next number in sequence.
// You can assume all tests inputs to be valid.
function nextVersion(version) {
const versionArr = version.split('.').map(el => Number(el));
for (let i = versionArr.length - 1; i >= 0; i -= 1) {
if (i !== 0 && versionArr[i] === 9) {
versionArr[i] = 0;
} else {
versionArr[i] += 1;
return versionArr.join('.');
}
}
}