<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Eval on blog.iankulin.com</title><link>https://blog.iankulin.com/tags/eval/</link><description>Recent content in Eval on blog.iankulin.com</description><generator>Hugo</generator><language>en-AU</language><lastBuildDate>Mon, 16 Jan 2023 00:00:00 +0000</lastBuildDate><atom:link href="https://blog.iankulin.com/tags/eval/index.xml" rel="self" type="application/rss+xml"/><item><title>Calculator</title><link>https://blog.iankulin.com/calculator-2/</link><pubDate>Mon, 16 Jan 2023 00:00:00 +0000</pubDate><guid>https://blog.iankulin.com/calculator-2/</guid><description>&lt;p&gt;I&amp;rsquo;ve been doing a bit of driving during the holidays, which means a lot of podcast listening. An episode of &lt;a href="https://topenddevs.com/podcasts/javascript-jabber/episodes/splatty-doo-and-other-javascript-features-you-should-avoid-jsj-543"&gt;JavaScript Jabber about JS features you should never use&lt;/a&gt; sparked my interest in &lt;code&gt;[eval()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval)&lt;/code&gt;. &lt;code&gt;eval()&lt;/code&gt; takes whatever you pass it in a string and executes it in the JS engine. This is a crazy concept if you&amp;rsquo;ve come from complied languages, and has obvious security implications. As with dynamic typing, I&amp;rsquo;m trying to force myself out of my comfort zone to embrace JS&amp;rsquo;s unique talents so I was keen to try &lt;code&gt;eval()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval"&gt;&lt;img src="https://blog.iankulin.com/images/screen-shot-2023-01-08-at-7.51.10-am.png" alt=""&gt;&lt;/a&gt;
&lt;em&gt;lol - challenged accepted.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;My first idea for using &lt;code&gt;eval()&lt;/code&gt;was to write a calculator. Pressing the buttons would make build a string, this could just be passed off to &lt;code&gt;eval()&lt;/code&gt; and the return value displayed. It&amp;rsquo;s such an obvious idea I&amp;rsquo;m sure I&amp;rsquo;m not the first to have it.&lt;/p&gt;
&lt;p&gt;To ensure I&amp;rsquo;m growing my CSS skills, I also decided to steal the design of the iPhone calculator. That&amp;rsquo;s the first one below. The second is my current web app version.&lt;/p&gt;
&lt;img src="https://blog.iankulin.com/images/img_3911.png" width="577" alt=""&gt;
&lt;img src="https://blog.iankulin.com/images/img_02a92dbcfb55-1.jpeg" width="577" alt=""&gt;
&lt;p&gt;Since the calculator display is used for two asynchronous purposes - showing the calculation string as it&amp;rsquo;s being built, and showing a calculation result when we press equals, I&amp;rsquo;ve kept a state variable &lt;code&gt;inputState&lt;/code&gt; which is true when we&amp;rsquo;re building the string, and false when we&amp;rsquo;re displaying a result. &lt;code&gt;btnAddClick()&lt;/code&gt; is attached to all the buttons used to build the string - &lt;code&gt;0123456789()-+/*&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#d8dee9;background-color:#2e3440;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;let inputText = &amp;#39;&amp;#39;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;let inputState = true;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;function btnAddClick(event) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; if (!inputState) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; inputState = true;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; inputText = &amp;#34;&amp;#34;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; inputText = inputText + event.target.innerHTML;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; txtOutput.innerHTML = inputText;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The backspace key just slices off the last character in the input string.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#d8dee9;background-color:#2e3440;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;function btnBackspaceClick() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; if (inputState &amp;amp;&amp;amp; inputText.length &amp;gt; 0) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; inputText = inputText.slice(0, -1);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; txtOutput.innerHTML = inputText;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Clear just empties the string and updates the display, then equals calls the dreaded &lt;code&gt;eval()&lt;/code&gt; and shows the output. To make it a bit fancy, I show the input for the calculation just above the result.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#d8dee9;background-color:#2e3440;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;function btnEqualsClick() {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; inputState = false;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; let output = eval(inputText);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; txtPrevious.innerHTML = inputText+&amp;#34;=&amp;#34;;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; txtOutput.innerHTML = output;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;That&amp;rsquo;s pretty much the entire code. Of course it doesn&amp;rsquo;t quite work like a conventional calculator, but I also didn&amp;rsquo;t have to learn anything about &lt;a href="https://en.wikipedia.org/wiki/Calculator_input_methods"&gt;Reverse Polish Notation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The big challenge - and you can see from the screenshots above, still ongoing - is the getting the CSS to work in a way that it looks correct on different devices. My iPhone is an SE, and I had it looking good on that, then sent it to a friend with a newer iPhone and the URL area would not hide. I&amp;rsquo;ll keep working at it, it has forced me to get a better understanding of grid.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m loving a browser developer tools to help with this. Both browsers have a &amp;ldquo;responsive mode&amp;rdquo; that allows you to resize the view to simulate phone like sizes without fiddling with your browser size all the time, and to still be able to see the tools. Dock your tools to the side, and look for the little phone/tablet button to get into responsive mode.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://blog.iankulin.com/images/screen-shot-2023-01-08-at-8.31.49-am.jpg" alt=""&gt;&lt;/p&gt;
&lt;p&gt;One other thing I learned is that in Safari on iOS double clicking on a web page zooms it in a little. That&amp;rsquo;s a great feature I guess, but a pain if you just want to enter a number like 99 on a web calculator. The solution turned out to be setting the CSS property &lt;code&gt;[touch-action](https://developer.mozilla.org/en-US/docs/Web/CSS/touch-action)&lt;/code&gt; on the buttons to &lt;code&gt;manipulation&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/IanKulin/Calc"&gt;source&lt;/a&gt; or &lt;a href="https://iankulin.github.io/calc/"&gt;try out the current version&lt;/a&gt;&lt;/p&gt;</description></item></channel></rss>