{"id":113,"date":"2022-09-16T17:12:57","date_gmt":"2022-09-16T17:12:57","guid":{"rendered":"https:\/\/mustafauzun.co\/blog\/?p=113"},"modified":"2022-09-17T08:56:52","modified_gmt":"2022-09-17T08:56:52","slug":"javascript-turn-function-function-to-string-based-calculator","status":"publish","type":"post","link":"https:\/\/mustafauzun.co\/blog\/javascript-turn-function-function-to-string-based-calculator\/","title":{"rendered":"Javascript, turn function to string based calculator"},"content":{"rendered":"\n<p>Like C#, also in javascript, it\u2019s pretty easy to do string-based math.<\/p>\n\n\n\n<p>By following the steps below, performing math operations or creating a simple calculator with the function class and regex can be done quickly.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>(Source:&nbsp;<a href=\"https:\/\/github.com\/mrna0\/calc.js\" data-type=\"URL\" data-id=\"https:\/\/github.com\/mrna0\/calc.js\">My Github<\/a>)<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>function calc(operation) {\n\n    if(!operation.match(\/[0-9%^*\\\/()\\-+.]\/g)) {\n        throw &#39;only use numbers and operators&#39;\n    }\n\n    \/\/remove all whitespaces\n    operation = operation.replace(\/\\s+\/g, &#39;&#39;)\n\n    \/\/if parenthesis on the side of the digit then replace\n    \/\/etc. 1(2+2) to 1*(2+2)\n    operation = operation.replace(\/(-?\\d)(\\()\/g, &#39;$1*$2&#39;)\n\n    \/\/etc. (2+2)1 to (2+2)*1\n    operation = operation.replace(\/(-?\\))(\\d)\/g, &#39;$1*$2&#39;)\n\n    \/\/etc. --+ to +\n    operation = operation.replace(\/\\-{2}\/g, &#39;+&#39;)\n\n    \/\/etc. ++ more operation letter to one +\n    operation = operation.replace(\/\\+{2,}\/, &#39;+&#39;)\n    \n    return Function(`return +${operation}`)()\n\n}<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Like C#, also in javascript, it\u2019s pretty easy to do string-based math. By following the steps below, performing math operations or creating a simple calculator<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,8],"tags":[16,31,30,9,32],"class_list":["post-113","post","type-post","status-publish","format-standard","hentry","category-c","category-javascript","tag-c","tag-calculator","tag-eval","tag-javascript","tag-regex"],"_links":{"self":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":5,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":120,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/113\/revisions\/120"}],"wp:attachment":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}