More fun with string concatenation

Last week, we looked at string concatenation and I figured that this week, we could see what you could actually do with it.  Back in 2014, I used a calculator to play around with PhoneGap.  It might be time to play with the concept again.

See the Pen
Vue.js Calculator – v1
by Joe Steinbring (@steinbring)
on CodePen.

In the above example, we have a value stored as “value” and as you click buttons on the calculator, it appends to the “value” string.  The eventual evaluation of the string’s numerical value is done using eval().  It is admittedly a much less than perfect calculator, though.  Once you hit “=”, the whole thing kind of breaks down.  Let’s try again.

See the Pen
Vue.js Calculator – v2
by Joe Steinbring (@steinbring)
on CodePen.

This example still uses addExpresion(), clear(), and the computed value of displayValue but it is less dependent on slice() and hacky cheats.

So, can we simplify this a little?  Sure!

See the Pen
Vue.js Calculator – v3
by Joe Steinbring (@steinbring)
on CodePen.

You’ll notice that in the third iteration, I removed an unnecessary condition (that was looking for the clicking of an operator button).

Have a suggestion of how to improve it further?  Feel free to drop a comment, below.

 

[ Cover photo by Kelly Sikkema on Unsplash ]

Leave a Reply

Your email address will not be published. Required fields are marked *