{"id":53593,"date":"2021-10-12T03:01:26","date_gmt":"2021-10-11T17:01:26","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=53593"},"modified":"2021-10-10T18:24:13","modified_gmt":"2021-10-10T08:24:13","slug":"javascript-incrementing-and-decrementing-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/javascript-incrementing-and-decrementing-primer-tutorial\/","title":{"rendered":"Javascript Incrementing and Decrementing Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/plus_avoids_zero.html\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Javascript Incrementing and Decrementing Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/plus_avoids_zero.jpg\" title=\"Javascript Incrementing and Decrementing Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Javascript Incrementing and Decrementing Primer Tutorial<\/p><\/div>\n<p>Sometimes writing Javascript code we&#8217;ve let code like &#8230;<\/p>\n<p><code><br \/>\n&lt;script type='text\/javascript'&gt;<br \/>\n  var counter=null;<br \/>\n<br \/> <br \/>\n  function initializeCounter() {<br \/>\n    counter=0;<br \/>\n  }<br \/>\n<br \/> <br \/>\n  function incrementCounter() {<br \/>\n    <b>if (counter == null) {<\/b><br \/>\n    <b>  counter=0;<\/b><br \/>\n    <b>}<\/b><br \/>\n    ++counter;<br \/>\n  }<br \/>\n<br \/> <br \/>\n  if (eval(Math.floor(Math.random() * 1746765765) % 2) == 1) {<br \/>\n    initializeCounter();<br \/>\n  } else {<br \/>\n    incrementCounter(); \/\/ if called first, is interesting<br \/>\n  }<br \/>\n&lt;\/script&gt;<br \/>\n<\/code><\/p>\n<p> &#8230; go through to the keeper.  There&#8217;s nothing <i>intrinsically<\/i> wrong with it.  But the other day, thanks to <a target=_blank title=\"Device Orientation \u2018alpha\u2019\nCalibration\" href='https:\/\/www.w3.org\/2008\/geolocation\/wiki\/images\/e\/e0\/Device_Orientation_%27alpha%27_Calibration-_Implementation_Status_and_Challenges.pdf'>Device Orientation \u2018alpha\u2019<br \/>\nCalibration &#8230; World-based calibration on iOS<\/a>, we learnt that the <b>&#8220;if&#8221;<\/b> bracketing above can be superfluous in the sense that that same code above could be written &#8230;<\/p>\n<p><code><br \/>\n&lt;script type='text\/javascript'&gt;<br \/>\n  var counter=null;<br \/>\n<br \/> <br \/>\n  function initializeCounter() {<br \/>\n    counter=0;<br \/>\n  }<br \/>\n<br \/> <br \/>\n  function incrementCounter() {<br \/>\n    ++(+counter); \/\/ ... more succinctly<br \/>\n  }<br \/>\n<br \/> <br \/>\n  if (eval(Math.floor(Math.random() * 1746765765) % 2) == 1) {<br \/>\n    initializeCounter();<br \/>\n  } else {<br \/>\n    incrementCounter(); \/\/ if called first, is interesting<br \/>\n  }<br \/>\n&lt;\/script&gt;<br \/>\n<\/code><\/p>\n<p>And so, we thought we&#8217;d write today&#8217;s proof of concept <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/plus_avoids_zero.html_GETME\">plus_avoids_zero.html<\/a>&#8216;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/plus_avoids_zero.html\">live run<\/a> link in order for a user to try out some &#8220;Prefix style&#8221; incrementing and decrementing of Javascript var<font size=1>iables<\/font> &#8230;<\/p>\n<p><iframe src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/plus_avoids_zero.html\" style=\"width:96%;height:400px;\"><\/iframe><\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d53593' onclick='var dv=document.getElementById(\"d53593\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/operator\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d53593' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes writing Javascript code we&#8217;ve let code like &#8230; &lt;script type=&#8217;text\/javascript&#8217;&gt; var counter=null; function initializeCounter() { counter=0; } function incrementCounter() { if (counter == null) { counter=0; } ++counter; } if (eval(Math.floor(Math.random() * 1746765765) % 2) == 1) { initializeCounter(); &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/javascript-incrementing-and-decrementing-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37],"tags":[3555,3815,3321,3814,3817,3290,652,752,3816,2269,997,1319,1721],"class_list":["post-53593","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","tag-decrement","tag-decrementing","tag-increment","tag-incrementing","tag-initialization","tag-initialize","tag-javascript","tag-mathematics","tag-null","tag-operator","tag-programming","tag-tutorial","tag-variable"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/53593"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=53593"}],"version-history":[{"count":3,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/53593\/revisions"}],"predecessor-version":[{"id":53596,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/53593\/revisions\/53596"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=53593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=53593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=53593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}