{"id":40,"date":"2018-05-13T15:07:52","date_gmt":"2018-05-13T15:07:52","guid":{"rendered":"https:\/\/www.lyndonantcliff.com\/learning-to-code\/?p=40"},"modified":"2018-05-13T15:07:52","modified_gmt":"2018-05-13T15:07:52","slug":"7-days-of-javascript-hell-over-a-few-lines-of-code","status":"publish","type":"post","link":"https:\/\/www.lyndonantcliff.com\/learning-to-code\/7-days-of-javascript-hell-over-a-few-lines-of-code\/","title":{"rendered":"7 Days of Javascript hell over a few lines of code"},"content":{"rendered":"<p>I wanted to create an original piece of code which did something.<\/p>\n<p>And I did with <a href=\"https:\/\/www.lyndonantcliff.com\/forest-mood\/design.html\">Chop Wood<\/a>.<\/p>\n<p>Something simple, but something which took an input and then changed an attribute on the DOM. Or is it &#8220;element&#8221;, I am still not well versed in javaspeak.<\/p>\n<p>This week has all been about learning a few simple things to the point of where I could actually use them:<\/p>\n<ul>\n<li>querySelector <\/li>\n<li>getElementById <\/li>\n<li>function()<\/li>\n<li>addEventListerner<\/li>\n<li>Global variables<\/li>\n<li>Function variables<\/li>\n<\/ul>\n<p>I have spent 7 days or more studying the above, in various youtube video tutorials, books and wikis. All I wanted to do was present and object with a variable, click on that object which reduces that variable by one and then changes another variable. I then wanted the variable that had been changed to be clicked on and change again, as well as changing another variable and so on.<br \/>\nCascading the effect down a list of objects.<\/p>\n<p>It seems simple, well I thought it did and found it incredibly hard, but I stuck with it. Although causing my family to emit radioactive death rays towards me as I wouldn&#8217;t stop going on about it. Ever been there?<\/p>\n<p>One of the problems is the much talked about issue of function variables and global variables, I thought it would be easy to pass a variable which originated in a function to play in the global scope, no no no, can&#8217;t do that. <\/p>\n<p>I read you could create the variable outside the function and then pass it in, but still I couldn&#8217;t get it to work.<\/p>\n<p>I then found a work around and came up with this:<br \/>\n<code><br \/>\nlet treecard = document.querySelector('.this-tree'),<br \/>\n    count = 10,<br \/>\n    logfun = 0;<\/p>\n<p>\/\/Tree counting function<br \/>\ntreecard.addEventListener('click', function() {<br \/>\n    \/\/tree<\/p>\n<p>    if (count <= 0) {\n        alert(\"You are out of trees, dumb dumb. Go find some more.\");\n        return\n    } else {\n        count--;\n        document.getElementById('tree-score').innerHTML = count;\n\n        logfun++;\n        document.getElementById('log-score').innerHTML = logfun;\n    }\n\n});<\/code><\/p>\n<p>I absolutely expect there to be an easier work around and may even be schooled by another javascript noob, which I am fine with this. I have only been serious about learning javascript for a few months so any criticism from coders more experienced than me is welcome.<\/p>\n<p>I still can't wrap my head around where the variable of \"count\" actually lives, is it on the DOM or is it in the script. And is a global variable(I assume it is) or a function variable?<\/p>\n<p>I have read a number of code examples of global vs function variables and become even more confused, but I know clarity will come. As it has somewhat with querySelector and getElementById and I get a kick out of manipulating the DOM with them. My question is, why bother with getElementById anymore. Is it just habit that people still use it?<\/p>\n<p>Also, getting to grips with ES6, which seems the wise thing to do. All the old tutorials still use var, whilst I am focusing on only using let and cost. Which seems to be the perceived wisdom. I realise it is worth getting to know the old way of doing things, especially if working on legacy cod.<\/p>\n<p>So go look at what I did, yes, it will take you 17 seconds to get it and then get bored, but I am so proud that I got it to work. Real agony was spent on this.<\/p>\n<p>I have new things to add updates to the Chop Wood game, which will continue my learning, like:<\/p>\n<ul>\n<li>Animation of objects when performing<\/li>\n<li>Sounds when objects performing<\/li>\n<li>Time delay for better usability<\/li>\n<li>Progress bar, time animated<\/li>\n<li>Regrow trees button with cost to score variable<\/li>\n<li>Market place to sell and buy items from the game, variable pricing<\/li>\n<li>Save game to JSON<\/li>\n<li>Save game to local storage on browser<\/li>\n<li>Save game to Firebase<\/li>\n<li>Save game to SQL<\/>\n<p>A long way to go yet, but I have set myself the goal of learning javascript this year. I expect the above to take another 3 -4 months to complete. But I love learning this stuff and creating something where nothing existed before, even if the actual thing is so simple.<\/p>\n<p>Any comments or suggestions please make them in the comments. Would love to hear feedback.<\/p>\n<p><a href=\"https:\/\/www.lyndonantcliff.com\/forest-mood\/design.html\">Chop Wood<\/a><\/p>\n<p>Thanks to the Youtube channels of <a href=\"https:\/\/www.youtube.com\/channel\/UC29ju8bIPH5as8OGnQzwJyA\">Brad Traversy<\/a> and <a href=\"https:\/\/www.youtube.com\/user\/pizzapokerguy87\">Dylan Isreal<\/a>, I bought Brad's Modern Javascript course from Udemy, highly recommend it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to create an original piece of code which did something. And I did with Chop Wood. Something simple, but something which took an input and then changed an attribute on the DOM. Or is it &#8220;element&#8221;, I am still not well versed in javaspeak. This week has all been about learning a few &hellip; <a href=\"https:\/\/www.lyndonantcliff.com\/learning-to-code\/7-days-of-javascript-hell-over-a-few-lines-of-code\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">7 Days of Javascript hell over a few lines of code<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.lyndonantcliff.com\/learning-to-code\/wp-json\/wp\/v2\/posts\/40"}],"collection":[{"href":"https:\/\/www.lyndonantcliff.com\/learning-to-code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lyndonantcliff.com\/learning-to-code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lyndonantcliff.com\/learning-to-code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lyndonantcliff.com\/learning-to-code\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":2,"href":"https:\/\/www.lyndonantcliff.com\/learning-to-code\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":42,"href":"https:\/\/www.lyndonantcliff.com\/learning-to-code\/wp-json\/wp\/v2\/posts\/40\/revisions\/42"}],"wp:attachment":[{"href":"https:\/\/www.lyndonantcliff.com\/learning-to-code\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lyndonantcliff.com\/learning-to-code\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lyndonantcliff.com\/learning-to-code\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}