DEV Community

Discussion on: JavaScript Katas: Higher Version

Collapse
 
rambou profile image
Nikolaos Bousios

But this is faster and simpler right?

function higherVersion(version1, version2) {
  // split the strings into numbers
  const split1 = version1.split('.').join('');
  const split2 = version2.split('.').join('');

  return Number(split1) > Number(split2);
}

// TEST CASES
const testInput01 = ["1.2.3", "1.2.0"]; // true
const testInput02 = ["9", "10"]; // false

// OUTPUT
console.log("--------------");
console.log(higherVersion(testInput01[0], testInput01[1]));
console.log(higherVersion(testInput02[0], testInput02[1]));
console.log("----------------------------");
Collapse
 
westim profile image
Tim West

With the assumption that the two versions are the same length and have no leading zeros, yes. I'd probably recommend .replaceAll('.', '') over split('.').join('').