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){
array1.push(1);
}
if ( condition_2 = true){
array1.push(2);
}
if ( condition_3 = true){
array1.push(3);
}

//(you get the idea)

if ( condition_A = true){
array1.push("+");
}
if ( condition_B = true){
array1.push("-");
}
if ( condition_C = true){
array1.push("*");
}
if ( condition_D = true){
array1.push("/");
}

(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.toString();
}
if (array1.length === 2){
array1value = array1.toString() +
array1.toString();
}
if (array1.length === 3){
array1value = array1.toString() +
array1.toString() +
array1.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:
534+902/21

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.

Tags:

## Discussions

The forums are retiring in 2021 and are now closed for new topics and comments.

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

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

but

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

Try to use the eval() function.

http://www.w3schools.com/jsref/jsref_eval.asp

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.

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

http://en.wikipedia.org/wiki/Parsing

What function do I use to return expressions in javascript? I am planning on using a for loop that will cycle through array, array, array, array, array 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?