{"id":525,"date":"2022-08-30T15:02:48","date_gmt":"2022-08-30T15:02:48","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/javascript-variable-reset-after-leaving-if-statement-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:02:48","modified_gmt":"2022-08-30T15:02:48","slug":"javascript-variable-reset-after-leaving-if-statement-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/javascript-variable-reset-after-leaving-if-statement-collection-of-common-programming-errors\/","title":{"rendered":"Javascript variable reset after leaving if statement?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m brand new to javascript with a C# background, so I was intrigued when I stumbled onto the following situation:<\/p>\n<p>EDIT: I&#8217;ve included the original text reproducing the issue. The alert text is showing as &#8220;undefined&#8221;. Am I correctly querying the radios?<\/p>\n<p><strong>index.html<\/strong><\/p>\n<pre><code>\n    \n        \n    \n\n    \n        \n            10 <br \/>\n            20 <br \/>\n            30 <br \/>\n            \n        \n    \n\n<\/code><\/pre>\n<p><strong>scripts.js<\/strong><\/p>\n<pre><code>function buttonPressed()\n{\n\nvar radios = document.getElementsByName(\"group1\");\nvar checkedRadio;\n\nfor(var i in radios)\n{\n    if(radios[i].checked)\n    {\n        checkedRadio = i;\n    }\n}\n\nvar tipAmount;\n\nif(checkedRadio == 0)\n{\n    tipAmount = 10;\n}\nif else(checkedRadio == 1)\n{\n    tipAmount = 20;\n}\nif else(checkedRadio == 2)\n{\n    tipAmount = 30;\n}\n\nalert(tipAmount);\n}\n<\/code><\/pre>\n<p>Why is alertText undefined?<\/p>\n<p>Thanks<\/p>\n<ol>\n<li>\n<p>JavaScript has no block scope, only function scope. So the code you gave us will work.<\/p>\n<\/li>\n<li>\n<p>This code should work, I suggest you to download <code>Firebug<\/code> so you will have access to the script and put a break point at the first line and press <code>F10<\/code> to debug on every lines and you should find the error quickly or tell us what it does.<\/p>\n<p>Can you try with a loop like this<\/p>\n<pre><code>for( i = 0; i &lt; radios.length; i++ ) \n{\n    if(radios[i].checked)\n    {\n       checkedRadio = i;\n    }\n\n}\n<\/code><\/pre>\n<p>Also you could use the value in your checkbox instead of hard coding the tip.<\/p>\n<p>Do you have jquery?<\/p>\n<p>You could do something easy like this<\/p>\n<pre><code>$('.group1:checked').each(function() {\ntip = this.val();\n});\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 20:18:44. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m brand new to javascript with a C# background, so I was intrigued when I stumbled onto the following situation: EDIT: I&#8217;ve included the original text reproducing the issue. The alert text is showing as &#8220;undefined&#8221;. Am I correctly querying the radios? index.html 10 20 30 scripts.js function buttonPressed() { var radios = document.getElementsByName(&#8220;group1&#8221;); var [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-525","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/525","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=525"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/525\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}