Author Options:

Javascript help? Answered

After figuring out how to use arrays and turning them into strings, I now need to do floating point operations with the variable.

To first I created an array:

     var = array1 = [];
     var = array1value = 0;
then, I add tack in numbers and operations when the right button is pressed, satisfying the corresponding if statement.

if ( condition_1 = true){
if ( condition_2 = true){
if ( condition_3 = true){

//(you get the idea)

if ( condition_A = true){
if ( condition_B = true){
if ( condition_C = true){
if ( condition_D = true){

(did you notice I had to add quotes to the operators? otherwise I get errors.)

Anyway, then I proceed to take the data inside the array, and 'compress'  it into one string of characters.

if (array1.length === 1){
    array1value = array1[0].toString();
if (array1.length === 2){
    array1value = array1[0].toString() +
if (array1.length === 3){
    array1value = array1[0].toString() +
                           array1[1].toString() +

etc. you can see the pattern (also can anyone tell me how to convert all this redundant code into a nice for loop? I couldn't figure it out, so I just did the above all the way up to array.length === 13)

Then finally, I make this appear as text on a output, and I can see the final value after pressing buttons and stuff, say, 534+902/21. So I would expect that if I save this in a new variable, that it will perform the math operation: except the new variable just returns the same exact thing: 

var newVariable = array1value

So I tried adding and subtracting some numbers from array1value, and the output of the newVariable is an error. I believe this is because although as text, array1value appears as 534+902/21, in actuality, it is *really*  

534 + "+" + 902 + "/" +21. or maybe as "534+902/21." How do I force javascript to convert this back to 534+902/21?

Also, please don't tell me to google it, or find java tutorials, I have already searched for half the day and don't want to watch 2 hour long videos on programming. 


This is pretty easy.

Simply use parseInt(string) or parseFloat(string)

var blah = "15"; // a string representing the value 15

var blah2 = parseInt(blah); // integer from string number

var blah3 = parseFloat(blah); // float value from string number

Depending on the implementation of Javascript, you may or not require a "Math" prepend on the statement , ie, Math(parseInt(string))

However, in most browser-based javascript work, it's unnecessary to use the math library to issue parseInt() or parseFloat().

parseInt converts a string input to a number


3 years ago

You concatenate numbers and strings (with the + operator). The result is a string where the numbers are casted to strings. It's not

534 + "+" + 902 + "/" +21


"534" + "+" + "902" + "/" + "21"

Try to use the eval() function.


I should mention that this is not good practice though. Casts are done implicitely to the string and it's all beyond your control ...

Yeah, seems so. The error message appears tellinging me eval can be 'harmful' https://www.khanacademy.org/cs/calculator/5724205359824896 :( Perhaps I am going about this in all the wrong way. Do you have any ideas? Can I somehow push plus, minus, add, and divide into the array without quorts?

Unfortunately I'm not that familiar with web development (rather with Windows Scripting and C++). However all I know is that you can't save + - etc. as operators in an array. They are always literal expressions in this case.

To get arount the eval() function is pretty complicated. Either you have to write your own parser or (if you don't want to reinvent the wheel) do some Google lookups for already existing parsers. E.g.


What is a parser and how does it work?

Basically a parser is an analyser for literal expressions according to defined syntactic rules. In your case you have to separate numbers from operators, you have to count opening and closing parentheses and depending on the result you have to devide the expression into separate calculation units. (Keep operator preceedence and parenthesis rules in mind.) The linked examle shows how it works. Also see


What function do I use to return expressions in javascript? I am planning on using a for loop that will cycle through array[0], array[1], array[2], array[3], array[4] etc and use a few (quite a few) if statements to decide what is in the value inside, then those values are tacked on a an output variable. How do I do that, in other words, if I want to add "2", "+" "2", and convert the individual terms so that to 2, +, 2, how do I build an expression with these?

You could like have it store a 0 for +, a 1 for - and map other operations to other numbers and when running have a switch with all the cases.

How would you separate the 0 for the +operator from a numeric 0 then? Eventually you would also need a parser that "translates" the 0 back to the operator which is the same as recognizing the literal "+", isn't it. (The latter would be unambiguously though.)

case 0:
val1 + val2;
case 1:
val1 - val2;

Of cource that is far from complete, it just shows what i am thinking

OK imagine your equation is

0 + 1 - 1

according to your rules the resulting array is

{0, 0, 1, 1, 1}

In case of only + and - you could probably do some validity checks to separate operators from numbers (which would complicate the parsing tremendously though). As soon as you implement more operators (* = 2, / = 3 or so) you wouldn't have any chance for a unique identification...

You could have "parenthesis" with arrays in arrays and parse that recursively by checking the variable type - or you could just make the thing have left-to-right math order.


3 years ago

Gees I did not think something so simple would turn out so difficult that I have to write a whole parser algorithm to just get rid of quotes and convert a string to an expression! Obviously I am very new to programming, and don't understand how the expression parser works. Can someone explain it in english or simple code?