Monday, May 25, 2015

JavaScript Array Reduce Method

JavaScript loops such as for and while provides a way to do useful work on each array element, i.e code below:
<script>
	var numbers = [5, 2, 15, 7, 9, 3, 8, 25, 3, 10, 11];
	for(var i = 0; i < numbers.length; i++){
		console.log(numbers[i]);
	}
</script>

The for loop goes through each element in the array and logs the value to the browser console.

Another useful JavaScript feature to go through each array element is by using the method reduce built-in on arrays. The method passes through each array element to reduce the values to a single value. This can be used to accumulate the element values into a single value.

The code below takes an array of numbers and uses the reduce method to get the element with the highest value divisible by the first element value:

The array numbers holds integer values for each of its element. The JavaScript code is to take the first element and divide the remaining elements by the value. The code checks if the quotient is divisible (no remainder), and determines which divisible value is the highest.

The array numbers has 5 as the value of the first element. Scanning the remaining elements, we see that there are three element values divisible by 5: 15, 25 and 10.
The variable divisor takes the value of the first array element. This is the value which array elements starting from the second up to the last will be divided by. 
The reduce method of the array has a callback function as its parameter. Callback functions, in turn, can have four parameters: previous value, current value, index and the array calling the reduce method.
In the code provided, only two parameters will be used. The start will hold the previous value passed to the callback, while the current will hold the current element in the callback. 

On the first loop of the reduce method, start parameter is initialized to the first array element value, 5 while the current parameter takes the second array element value, 2:
The current value, 2 is divided by the divisor value, 5. Since divisor was declared outside the callback method, the method can "see" or access the divisor. Since the quotient will return a value, the condition check will return a True value. The True value is reversed, and the condition check is evaluated to false. the first loop of the reduce method will then return the start value of 5.

On the next loop, the reduce method will assign the next element value, 15 to the current parameter. The start parameter takes the returned value of the previous loop, 5. The quotient of the current value 15 over the divisor, 5 will yield a value of zero, corresponding to false, which is then reversed to true. 
The check of current value, 15 greater than start value, 5 will evaluate to true. The current value, 15 is then returned. On the next, loop, the start parameter will now take this value.

On the loop that follows, the reduce method will assign the value of the next array element, 7 to the current parameter. The start parameter takes the returned value from the previous loop, which is 15. The quotient of the current value, 7 over the divisor, 5 will yield a value, corresponding to true, which is reversed to false. The loop will return the start value, 15.
 

The start value of 15 is retained, until the reduce method assigns the current parameter to array value 25. The loop will then return the value 25.
The start value of 25 will be retained even until the reduce method loops through the last array element. When the reduce method reaches the array element value 10, the loop determines that 10 is divisible by 5. However, the check of current value 10 greater than start value will evaluate to false. The loop will still return the value 25.
The full code can be accessed on my GitHib account:

Resources on array reduce method may accessed on the links below:



Saturday, May 16, 2015

Bootstrap Cheat Code for WordPress Thumbnail Mobile Responsiveness

Using the_post_thumbnail() in a WordPress loop displays the post featured image as a thumbnail to the post article. The first parameter size lets setting or customizing the width and height dimensions for the thumbnail. However, I wanted a flexibility for my Bootstrap based WordPress theme wherein the post thumbnail will display in a smaller size in mobile. In short, I wanted a post thumbnail mobile responsiveness on its display. The WordPress loop uses a Media Object to display the title, time posted, excerpt and of course, the thumbnail. A sample loop is shown below:
The Media Object calls the_post_thumbnail() while passing the 'thumbnail' parameter. The 'thumbnail' size is set to 150x150:

When the theme is rendered, WordPress set the width and height attributes to 150. I could have used CSS3's media query, but was not sure is it could have overwritten the width and height attributes. I decided come up with my own cheat code to display the thumbnail in a different size on mobiles:
The cheat involves using two image links. The first image link passes 'thumbnail' as the size parameter. The classes "hidden-xs hidden-sm" makes sure that the image will not display on mobile sized devices. This makes sure that the image is displayed in traditional desktop or laptop computers. The second image link passes array(50,50) as the size parameter. The thumbnail is displayed in 50px width and height. The classes "hidden-md hidden-lg" makes sure that the image will not display on computers with medium or large screens, i.e. desktop or laptop computers. This makes sure that the image is displayed on mobile devices. The WordPress loop code with the Bootstrap cheat is listed below
<?php 
if(have_posts()): 
while (have_posts()): the_post(); 
?>
<div class="media">
 <a href="#" class="pull-left hidden-xs hidden-sm">
  <?php the_post_thumbnail('thumbnail', array( 'class' => 'media-object img-rounded' )); ?>
 </a>
 <a href="#" class="pull-left hidden-md hidden-lg">
  <?php the_post_thumbnail(array(50,50), array( 'class' => 'media-object img-rounded' )); ?>
 </a>
 <div class="media-body">
  <h4 class="media-heading"><?php the_title(); ?> <small>Posted on<?php the_time('F j, Y'); ?></small></h4>
  <p><small><?php echo(get_the_excerpt()); ?></small></p>
  <p><a href="<?php the_permalink(); ?>" class="btn btn-primary">Read More</a>
  </p>
 </div>
</div>
<?php endwhile; ?>
<?php else: ?>
<?php endif; ?>