JavaScript: Compare versions

Last update: 10 Sep 23:20
2
Students
50%
Completion rate

solution.js

Write and export as default the function comparing given versions, version1 and version2. If version1 > version2, the function should return 1, if version1 < version2 then -1, and if version1 === version2 then 0.

Version is a string containing two numbers (major and minor versions) separated by a dot, e.g. 12.11. Note that a version is not a floating-point number but several unrelated numbers. To test if a version is higher/lower you should compare each number independently. Thus, version 0.12 is higher than version 0.2.

Example of versions order:

0.1 < 1.1 < 1.2 < 1.11 < 13.37

Examples

compareVersion("0.1", "0.2"); // -1

compareVersion("0.2", "0.1"); // 1

compareVersion("4.2", "4.2"); // 0

Tips

  • More on versioning: http://semver.org/

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests