0

Tips for replacing letters?

In my overdone calculator i've added tons of mathematic operators and constants etc. I can type in about ~35 letters, then it stops. When I use Math.sqrt(x) it uses a minumum of 12 letters. How do I hide the Math.sqrt part and replace it with a _/- instead?

21st Aug 2016, 4:45 PM
Oscar
Oscar - avatar
16 Answers
0
String.replace would probably be the easiest way to go for directly replacing characters.
21st Aug 2016, 5:16 PM
LFP6
LFP6 - avatar
0
Will the Math.PI/Math.Pow() or Math.sqrt still work?
21st Aug 2016, 5:17 PM
Oscar
Oscar - avatar
0
Im not sure what youre trying to do exactly. Could you give me more info about your project, or preferably a link to some code?
21st Aug 2016, 5:20 PM
LFP6
LFP6 - avatar
0
I'm making a calculator that can calculate and use: Pi Eulers Constant Log Square Root x^y (pow) modulus (%) use parantheses assign values to x and y and then use them (not working yet, because the Enter button isnt working) Division, multiplication, plus, minus Delete single characters. Reset the calculator. Use the Ans function. Calculate floats (decimals) Round decimals to integers And i'm trying to make so that it doesn't show the Math.Sqrt in the input window. I just want to see the _/- thingy and I want it to still be able to calculate Square Root. It's a bit hard to show all code because I got 600+ lines of it.
21st Aug 2016, 5:26 PM
Oscar
Oscar - avatar
0
Ok, what I need to know is how the user is entering the values (just typing?), and how is that calculation being done?
21st Aug 2016, 5:55 PM
LFP6
LFP6 - avatar
0
The user clicks on buttons. (I've assigned functions to all buttons that add the number/operator to the input field.) The calculation is being done by pressing the = button. function Equal() { cal.result.value=eval(cal.in.value); }
21st Aug 2016, 5:59 PM
Oscar
Oscar - avatar
0
You could always swap back and forth between. My suggestion though would be to have an internal representation of the calculation instead of pulling directly from the input.
21st Aug 2016, 6:15 PM
LFP6
LFP6 - avatar
0
Any tips on how I would do that? I use 2 visible areas in the main calculator. (I got 3 things next to eachother; A big thing with advanced functions, the main calculator and some tips and notes.) The 2 visible areas are: In and Result. Should I add a hidden area somewhere for the main input and use slice() and replace for the "fancy" input?
21st Aug 2016, 6:19 PM
Oscar
Oscar - avatar
0
I would just add a separate variable with the JS representation.
21st Aug 2016, 6:22 PM
LFP6
LFP6 - avatar
0
How? I'm a bad JS programmer. Started with JS ~2 weeks ago. Code for the Math.sqrt input (JS): function Sqrt() { cal.in.value+="Math.sqrt("; } //Note: The right paranthese isn't there because the user has to input a value. HTML: <input type="button" class="button" onclick="Sqrt()" value="_/-"> <--Note: I use the real Square Root sign in the calculator.
21st Aug 2016, 6:23 PM
Oscar
Oscar - avatar
0
You just need something like var expression = "" Then when, say, the user clicks square root, you concatenate math.sqrt to expression, but add the symbol to the display. Does that make sense?
21st Aug 2016, 6:29 PM
LFP6
LFP6 - avatar
0
It makes a little sense. so: I click _/- and then 9 and ) In the Expression input it shows Math.sqrt(9) But in the visible input window it shows _/-. And the code:? function Sqrt() { cal.exp.value+="Math.sqrt("; cal.in.value+="_/-("; }
21st Aug 2016, 6:35 PM
Oscar
Oscar - avatar
0
Yep!
21st Aug 2016, 6:37 PM
LFP6
LFP6 - avatar
0
Thank you! Adding this to my code now.
21st Aug 2016, 6:40 PM
Oscar
Oscar - avatar
0
It's not working. I added the new input, but it doesn't work. No buttons work. I've checked my JS and I haven't forgot to close any curly brackets. <input type="text" readonly size="20" name="exp" placeholder="Test" hidden> <input type="text" class="in" readonly size="20" name="in" placeholder="Input.> Some JS functions: function Random() { cal.in.value+="Math.random()"; cal.exp.value+="Random()"; } function Comma() { cal.in.value+=","; cal.exp.value+=","; }
21st Aug 2016, 7:17 PM
Oscar
Oscar - avatar
0
Well, personally I would use a variable instead of an input as I described. Aside from that, where is cal defined?
22nd Aug 2016, 3:08 PM
LFP6
LFP6 - avatar