Jonnie Grieve Digital Media: Website Blog.

Check out my very latest blogs. For more on particular topics check out the post categories.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *