Skip to main content
1 of 3

JavaScript (ES6) (Regex)

Regex is great and it can do many things. It can even do maths!

function tip(m) {
    var s = 'x'.repeat(m.replace('.', '')) + 'xxxx';
    var a = s.match(/^((x*)\2{99}(x{0,99}))\1{4}x{0,4}$/);
    var c = a[3].length;
    if (c < 9) c = '0' + c;
    return a[2].length + '.' + c;
}

All instances of *, /, + are not related to math operations.

  • + is string concatenation in all instances it is used.
  • * is part of RegExp syntax
  • / is the delimiter of RegExp literal

The input must use . as decimal point, and there must be 2 digits after decimal point. The function expects String argument, rather than Number.

Golf version comes later