Day 21 -23 , (Jan 10 – 12)

Code War Challenges

  1. Insert Dash
  2. Vampire Numbers
  3. All, None, Any –   Functional Programming
  4. Triangle sum of odd numbers    *** This is a great question!
  5. Narcissistic Number
  6. Title Case
  7. Build a Calculator – Calculating averages

Two More Closure problems

  1. Closure – twice
  2. Closure – Alternates

As a self-taught novice JavaScript programmer, I found that closures, callbacks, recursions and functional programming can be confusing at times. I wonder if there is any relationship between each of them. Over the past two months, I came across these four terms occasionally at different online learning platforms. I learned closure is extensively used in Node.js while callback is frequently used in jQuery. Recursion and functional programming are two programming techniques one can use creatively when solving coding challenges.

I have not yet read anything in depth on each of the topics above except closures. However, I plan to search for more online articles as well as programming books in order to understand each and their applications in JavaScript programming.

Here I found a good article about the callback (higher-order) functions. It answered the confusion I had of callbacks, closures and functional programming.

Understand JavaScript Callback and use them

According to the article,

  • Callback functions are derived from a programming paradigm known as functional programming. At a fundamental level, functional programming specifies the use of functions as arguments.
  • One of the chief techniques in functional programming happens to be callback functions.
  •  Callbacks are closures. A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. Link about closures
  • Callbacks are used for asynchronous execution such as reading files, making HTTP requests. Callbacks are used in Event Listeners/Handlers, setTimeout and setInterval methods.

Over the past few days, I also read four articles on Coding House’s Blog  which improved my understanding of some JavaScript array methods as well as object iteration.

  1. Counting, split(), and Objects

    I learned how to create objects within an object and how to access the inner object’s properties. I also learned how to add keys and values to the object by setting up the following function:

    var superCounter = function(str) {
        var counterObj = {};
        return counterObj;
    console.log(superCounter("Count me in"))
  2. Swapping Cases, Iterators, and TernariesI learned how to use the forEach iterator and set the iterator up with an index by passing in an “i” as an argument. I also learned an alternative way to replace “if…statement” which is by using ternaries. Below is a good summary of code that I learned from this blog post:
    var swapCase = function(str) {  
    var swapArray = str.split(' ');
    swapArray.forEach(function(e, i) {
    var swapped = (e[0] === e[0].toUpperCase()) ? 0: 1;
    swapArray[i] = swapped;
    return swapArray.join(' ');
    console.log(swapCase("ThEre is so MuCH to DO"));

3.  Reverse() and Map()

I learned the difference between and array.forEach(). applies a callback on all elements of an array and returns a NEW array, but forEach() applies a function once for each item in an array and doesn’t return anything.

I also learned that Javascript’s map() method creates a new array by calling a callback function on every element in the provided array. on its own does not alter the array on which it is called but the callback function does.

    4.  Function Arguments, Object Iteration, and Some Math

I learned the iterator that work with objects. The for – in loop which allows us to access both the  objects’ keys and values easily. I also learned the JavaScript reserved word ‘arguments‘ which is an object and therefore we cannot use any Array.prototype method such as Array.sort() on ‘arguments’. To convert the ‘arguments’ into an array of numbers, for-in loop becomes very useful.  See the example below from the blog.

var median = function() {  
  var numbers = [];
  for(var key in arguments) {
console.log(median(7, 2, 3, 8, 5));  

At the end of the blog, the writer posted a challenge. Solve the problem 
if an even number of arguments is being passed into the function. 
Instead of returning 'undefined', return a correct median. 
// Below is My Solution.

 var median = function() { 
 var numbers = [];
 for(var key in arguments) {
 numbers = numbers.sort(function(a, b) {
 return a - b;
   return numbers[(numbers.length-1)/2];
   return (numbers[(numbers.length)/2]+numbers[(numbers.length)/2-1])/2
median(7, 2, 3, 8, 5,9); ==> return median '6'

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s