Jonnie Grieve Digital Media: Website Blog.

Archived Posts: November 2017 – Jonnie Grieve Digital Media posts.

100 Days of Code: Day 6 Write-up

Project 5 – Using forEach, iterate over the percentages and store all percentages over 50 percent in the array upperRange.

We’ve reached Day 6.  I did try for “Day 6” being the Saturday just gone however numerous things conspired against that. So I wrote most of this write up a few days ago.  It was generally a successfull attempt; barring a couple of quirks I’ve managed to get through both of these JavaScript forEach challenges.

When I started this, I  was either returning an empty array in my console.log or the number 30.

With things still fresh in my mind from previous sessions, I’m testing for values below above 50 in the array and discarding the rest and trying to use the parameter variable to store the results of calculations.

I felt like I was very close but still far away from the answer.

I certainly have the understanding this time of what needs to be done, but implementing it is the tricky part.  I’ve attempted it enough times so let’s see how the solution compares.

This was perfectly acceptable for the challenge.  But inside the code block was the trouble.   I tried to apply thought and the process of elimination to get the answer.   I tried every combination… except the right one.

 

Since we know we’re calling the forEach method on the percentages array. we don’t need to pass it into push method.  We use the current value parameter for that.

SIX  Using forEach, iterate over the colors array and store the hex colors that start with the letter F (ex. #FF0000) in the array filteredColors.

Again, this challenge is very similar to the previous one. Starting with an array of hexadecimal strings and an empty array for the filtered values to be added to.

Thinking back, I can’t remember too much trouble getting the right answer. Except that for whatever reason, I couldn’t get the console.log to appear in the terminal.  console.log(filteredColors); should return the filtered colours but for no reason that I can see nothing happens. I verified the accuracy of the code having applied it to the code challenge checker.

I’ve even passed it through with 2 alternative answers… a solution that uses the startsWith() method and the other with charAt().

That said, when I bring the code over to a local file on my laptop, it’snot adding anything to the new array even though there’s are no runtime errors in the code.  I’m close, but it’s more than that, I’m seeing a runtime error returned for the first time.  My thought is that it might be a deprecated feature in node.js.  My version of Node is 8.6.0 but in Treehouse Workspaces, it is functional in version 5 of Node.

The final version of my solution uses a shortenecd syntax of the charAt()  method.

 

100 Days of Code: Day 5 Write-up

Whether I’ll have time to commit to 100 continues days of this challenge I’m not a sure. But I intend to complete the 100 days. So for the final day of the working week, I went back into my exploration of the JavaScript foreach method. I tackled the third and fourth of a set of 7 JavaScript scenarios.

Challenge 3 – “Turn the number of strings from an array into floats and add them.”

So in this problem. there’s a string array and a variable with an integer of 0. Very quickly I realised I’d needed the parseFloat() method; the method that converts values to the correct data type.

Did I need to go further and use a reducer? My initial thought was no, as these challenges (by my memory) don’t cover the use of one of those advanced array methods.

I felt like I was, no matter how much I look at the code, in need of some help. So before I talk about how the walkthrough went, and if it made sense to me, this was my attempt. For context, this is the array I needed to call foreach on and my attempt.

I was closer to the solution than I thought.

With stringPrices.forEach()  – apparently I didn’t even need to assign the call to forEach to a variable, the call to foreach would hapilly exist on its own.

I correctly remembered parseFloat to get the values in the proper way.

So we use stringPrice as the parameter of the anonymous callback function as the way to change the data type of the values in the array. Which means we’re now ready to start totaling up the numbers.

To do that,  we simply define a constant variable at uses the parameter to pass the values from the stringPrice  array to pass in the values.

It’s a little ironic how simple the calculation is; adding the value of one variable to another, which is what has happened in the next part of this challenge. But in this case, I was thinking about more complicated code than it needed to be.

I was on the right track to using += to output the sum of an array, I was just going about it the wrong way.

The use of stringPrice  also important and relevant to how the code works. I talk more about that further down in this post.

Challenge 4: Using forEach, iterate over the alphabet array and store each letter in the array noel except for the L character.

This challenge started with an empty array and string with the split() method called on it, which turned it into an array of single characters called alphabet.

My thought with this one was to use push to populate the empty noel array with everything initially, then identify the position of the “L” value in the new noel  array attempting to remove that index from the group.

Again, I don’t seem to have an issue working out which methods I need to use to solve a problem, and I had an inkling I should have used a condition statement. But, this is what these practice sessions are all about. To get me thinking about what can be done with these methods and practice with theme.

The main solution (I’m sure there are others) is below.

For me, the biggest takeaway is this….If I wasn’t sure about the purpose of current value parameters in these callback functions,  I am now. It’s the way JavaScript iterates values in loops by applying them to a temporary variable.  It’s a variable of any name that can be used in calculations.  That knowledge, that refresher is more valuable to me than memorising any piece of code.

100 Days of Code: Day 4 Write-up

I went back to Android Development today and the biggest challenge for me, hooking up the ListView with and Android adapter.  For the longest time I didn’t think I was going to get much done, with my connection speed and my struggles with the more complicated aspects of Android like ListAdapters.

The time served as a refresher for me on these things, for example that aListAdapter, a kind of bridge between the list view and the class that converts the view into data behind the scenes.

 

After having typed this all in I thought to myself… “Okay. Progress. I think I’ll leave it here for today”.

Today was another tricky time because I’ve been following on, from Treehouse trying to make this Golf Scorecard App. But I always knew this was going to be the toughest part because I just don’t have enough Android knowledge to attempt this part on my own. I was supposed to be attempting it myself before following the video but, I felt I couldn’t.

Part of that is my Dyspraxia, the other is just not having enough time in the day to practice and build up the self-confidence that I need. I thought, many times about just giving up and accepting I’m not cut out for this. But giving up is always the easy way out and I’m not giving up on my dream of making my own app. One way or the other, I’ll do it.

On the plus side, however, I’m really pleased that having followed on with the code, made my list adapter, I was able to get rid of an exception that caused the emulator to crash the app.

It took a little bit of research and carefully reading the exception runtime errors, and runtime errors generally scare me.

It went away after I assigned the list view its special id

android.R.id.list   and in the XML @+id/android:list

Now, I’m going to have to take some time to read through the code I just typed in and see if I can get some things to tick. But I can see a little bit more how practice can get you more familiar with Android Studio’s behaviours and make you more comfortable with the program.

So, while I’m still not at the stage where I’m writing my own code comfortably and unaided… all that means is I wouldn’t trust myself to write Android code for an employer but I can learn to get by making my own projects.

100 Days of Code: Day 3 Write-up

For day 3 of “100 days of code” challenge, I turned my attention to practicing forEach loops in JavaScript. All I’m doing today is running through some practice scenarios that use forEach methods.  I’ll do a couple today, for this post.

The first scenario, taken from Treehouse is this…. “Using forEach, add together all the elements in the numbers array, and store the total in the variable total.”

Very straightforward. At first, I tried something like this…

Off the top of my own head…. and it was wrong. I think I was thinking about forEach in PHP, or another iteration method in JS. Anyway, it was wrong.

What I’d wish I’d thought to do was check out the documentation. I’d have seen then, that the forEach method, when assigned to an array works by taking a callback function. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Using the new ECMAScript syntax the forEach method looks more like this:

So you have a given array which you call forEach.

– if you want to iterate over an array, that, in its simplest form is how you would do it.

What you then need is an anonymous callback function with a variable as an “iterator” variable.

Taking all this into account, my solution to the challenge was as follows.

Another solution is

numbers.forEach(number => total += number);

It’s essentially the same solution. I merely opted to use curly braces to make clear where the functional part of the anonymous function takes place.

The second challenge, as expected was a little more complicated.  The task said, “Using forEach, copy only the first 2 characters of each string in the days array and store the abbreviations in the dayAbbreviations array“.  But armed with the refreshed knowledge of how to use the method, I got started with

Once I got that far though, I got stuck.

Since there’s an empty array alongside that code, it seemed to me we want to populate array initially with the values of the days array. Then, of course, we need a string manipulation method to retrieve only the first 2 characters of each array value. So I opted for push and substring, hoping to cut array values from the beginning of each string to show only 2 characters.

Substring was okay but it turns out slice() was a better method to use. I’m quite pleased that I thought of push and string manipulation. However my implementation needed a lot of work. The final solution was as follows.

Again, I added console.log() to the function to prove the values were being correctly output.

100 Days of Code: Day 2 Write-up

Today I started actually coding  stage of this project. In actuality  this stage was relatively straightforward but I will admit to feeling the blank canvas syndrome.  As it stands only 2 fields needed to be added.  One for stroke label on the app and another to hold an integer for the stroke count. The app is going to need a new Java class file where the code for the functionality will live. Or for the most part at least.

So that’s the next step. I’ll create one in Android Studio.

It’s in this class we do 3 simple tasks.

  • declare fields
  • hole constructor
  • getter and setter methods

Once that is done the object is created. I was a little thrown by how easy this was.  You’ll need to know which data types will be declared and appropriate names for the field but in projects like this, Android Studio will be able to generate the constructor and getter and setter methods for you.

I’ll then initialise an array of Golf Holes.  This will be an array of the type Holes Now this one of the things I got tripped up on.  What the project requires is an declared array of a given number of indexes, not an array of empty strings like I tried to create.

not

And that’s it. I now have a main activity, a list item activity and a Java object that stores Golf stroke counts.  I think what has been done so far covers the very basics of developing an Android App.  We’re moving into some more advanced things next like setting up a List Adapter.  Now, I will attempt this on my own first but I intend to create an app and if I have to follow along to get there… well..  I intend to learn from the inevitable mistakes I’ll make too.  🙂

 

 

100 Days of Code: Day 1 Write-up

So, alright, we haven’t much started with coding yet so it is a little bit of a cop out for my first write up. There’s a couple of reasons for this.

Firstly each project needs time to be planned out; you will start an Android project by creating at least an initial outline of how an Android App will look. You will place your Views (or Objects), assign them appropriate ID’s and write XML code.

Secondly, I wanted to spend this time to prepare because at our home at the moment our broadband connections aren’t great and we need good speed for every time we run a Gradle sync and which involves retrieving information the Android Studio API’s. It makes sense for me to make sure the Android Activities are ready as can be for programming.

Lastly, part of the reason I’m doing this “100 Days of code” is an attempt to facilitate my learning process. The course I’m following on Treehouse guides you through the creation process on this app. The idea is to attempt what needs to be done on my own; fix any errors and if needs be, follow the walkthrough and compare it with my own attempts. I certainly don’t intend to create a like-for-like copy of the app and want to give my own take on it.

I’ll be doing the same for my JavaScript forEach side project too and beyond.  We all learn in our own ways and doing things this way allows me to learn in a way that’s best for me.

100 Days of Code: introduction

Hello, welcome to my first post for my 100 Days of code effort. I’m hoping to keep this going for as long as I can and want to see it through although maybe not for 100 consecutive days.

I have a couple of blogs planned that will hopefully explain a bit more about where I’m coming from, the kind of coder I’m going to be and why I want to take this up in the first place.

My social media accounts and blogs are somewhat bare because I’m so focussed on my own projects and I can’t afford anyone to do it for me so this is one thing that’ll help me to keep in touch with you all 🙂

To kick things off there’s a couple of things I’m going to put my focus on. These are an Android App for creating a Golf Scorecard and practicing JavaScript forEach methods. Where else the 100 days of code effort goes I’m not sure but this has been a long time coming so let’s make a go of it.

You can follow my progress from this blog and my GitHub page.