2

I am creating a website that inputs code in TI-BASIC (a built-in, somewhat crappy programming language on the TI-83+/TI-84+ calculators) and attempts to optimize it.

One common optimization is to replace, say, 10000 with 1e4, (more examples: 5500000 to 55e5, .0005 to 5e-3) where 'e' does the same thing as the "*10^(" part of scientific notation. (I hope that is understandable)

This only saves space if there are more than 3 zeros, (numbers are one byte, the 'e' token is 2 bytes) so I do not want to replace the numbers if they have less than 4 zeros.

Because the TI-Calculators only store 14 digits before or after the decimal, I should not need to worry about numbers with more than 14 digits.

I have been using JS RegEx to do a find and replace for all of the other optimizations on the site, so I was experimenting with using that for this problem, but I don't know if there is a better way.

I am not the least experienced with JS RegEx, so any help (even prodding in the right direction) is useful.

Thank you for your time.

iPhoenix
  • 719
  • 7
  • 20
  • Note, [E-notation](http://www.java2s.com/Tutorials/Javascript/Javascript_Tutorial/Data_Type/How_to_write_Scientific_notation_literal_in_Javascript.htm) is not scientific exponential notation. E-notation is an artifact of JavaScript language, see https://stackoverflow.com/a/46517494/ at [Pitfalls with using scientific notation in JavaScript](https://stackoverflow.com/q/46366778/) – guest271314 Nov 04 '17 at 18:45
  • Yes, I know that. I was referring to the E-Notation used in TI-Basic. It is slightly slower than using numbers, but it is generally smaller. – iPhoenix Nov 04 '17 at 18:49

1 Answers1

1

You could use Number#toExponential and eliminate the plus sign, if necessary.

function sansPlus(s) {
    return s.replace('+', '');
}

console.log(sansPlus(10000..toExponential()));
console.log(0.00001.toExponential());
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392