|
| 1 | +// naive solution... |
| 2 | +// function compare(a,b){ |
| 3 | +// const numA = +a; |
| 4 | +// const numB = +b; |
| 5 | +// if (numA < numB) return 'less'; |
| 6 | +// if (numA == numB) return 'equal'; |
| 7 | +// if (numA > numB) return 'greater'; |
| 8 | +// } |
| 9 | + |
| 10 | +function compare(a, b, flipResult = false) { |
| 11 | + if (a[0] === '-' && b[0] !== '-') { |
| 12 | + return 'less'; |
| 13 | + } else if (b[0] === '-' && a[0] !== '-') { |
| 14 | + return 'greater'; |
| 15 | + } else if (a[0] === '-' && b[0] === '-') { |
| 16 | + flipResult = true; |
| 17 | + a = a.slice(1); |
| 18 | + b = b.slice(1); |
| 19 | + } |
| 20 | + |
| 21 | + while (a[0] === '0') { |
| 22 | + a = a.slice(1); |
| 23 | + } |
| 24 | + |
| 25 | + while (b[0] === '0') { |
| 26 | + b = b.slice(1); |
| 27 | + } |
| 28 | + |
| 29 | + let aDecimal; |
| 30 | + let bDecimal; |
| 31 | + [a, aDecimal = ''] = a.split('.'); |
| 32 | + [b, bDecimal = ''] = b.split('.'); |
| 33 | + |
| 34 | + // if the length of a is longer than the length of b |
| 35 | + if (a.length > b.length) return flipResult ? 'less' : 'greater'; |
| 36 | + |
| 37 | + // if the length of a is the same as b |
| 38 | + if (a.length === b.length) { |
| 39 | + for (let i = 0; i < a.length; i++) { |
| 40 | + const digitA = a[i]; |
| 41 | + const digitB = b[i]; |
| 42 | + if (digitA > digitB) { |
| 43 | + return flipResult ? 'less' : 'greater'; |
| 44 | + } else if (digitA < digitB) { |
| 45 | + return flipResult ? 'greater' : 'less'; |
| 46 | + } |
| 47 | + } |
| 48 | + if (aDecimal || bDecimal) { |
| 49 | + while (aDecimal[aDecimal.length - 1] === '0') { |
| 50 | + aDecimal = aDecimal.slice(0, aDecimal.length - 1); |
| 51 | + } |
| 52 | + |
| 53 | + while (bDecimal[bDecimal.length - 1] === '0') { |
| 54 | + bDecimal = bDecimal.slice(0, bDecimal.length - 1); |
| 55 | + } |
| 56 | + |
| 57 | + while (aDecimal.length < bDecimal.length) { |
| 58 | + aDecimal += '0'; |
| 59 | + } |
| 60 | + |
| 61 | + while (bDecimal.length < aDecimal.length) { |
| 62 | + bDecimal += '0'; |
| 63 | + } |
| 64 | + return compare(aDecimal, bDecimal, flipResult); |
| 65 | + } |
| 66 | + return 'equal'; |
| 67 | + } |
| 68 | + |
| 69 | + // if the length of a is shorter than the length of b |
| 70 | + if (a.length < b.length) return flipResult ? 'greater' : 'less'; |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +console.log(compare('2','12'),'less'); |
| 75 | +console.log(compare('2','132'),'less'); |
| 76 | +console.log(compare('12','13'),'less'); |
| 77 | +console.log(compare('875','799'),'greater'); |
| 78 | +console.log(compare('1000','1000'),'equal'); |
| 79 | +console.log(compare('999','1000'),'less'); |
| 80 | +console.log(compare('123','122'),'greater'); |
| 81 | +console.log(compare('1000000000000000000000000000000000','1000000000000000000000000000000001'),'less'); |
| 82 | +console.log(compare('1000000000000000000000000000000002','1000000000000000000000000000000001'),'greater'); |
| 83 | +console.log(compare('0000000000000000000000000000000010000000000000000000000000000000000','0000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000001'),'greater'); |
| 84 | +console.log(compare('012','13'),'less'); |
| 85 | +console.log(compare('0000000.12','-120'),'greater'); |
| 86 | +console.log(compare('000000000000000876','000999'),'less'); |
| 87 | +console.log(compare('1000.00','1000'),'equal'); |
| 88 | +console.log(compare('999','-00000000000001000.00'),'greater'); |
| 89 | +console.log(compare('123.09','123.08'),'greater'); |
| 90 | +console.log(compare('-123.09','-123.08'),'less'); |
| 91 | +console.log(compare( '1000000000000000000000000000000002.00009','-1000000000000000000000000000000003'),'greater'); |
| 92 | +console.log(compare('2.40', '2.4'), 'equal'); |
| 93 | +console.log(compare('13.810', '13.701'), 'greater'); |
| 94 | +console.log(compare('13.000000001', '13.1000000000'), 'less'); |
0 commit comments