Practical PHP vs Academic JavaScript

13 05 2017

Flickr: by redvers

 

The following represent two questions that I came across at another site, one involves a practical PHP matter and the other represents a JavaScript classroom exercise.  I enjoy both kinds of problems and present some answers I devised.

One query concerned how to do an operation on a given date string which needs to change the year of that date back to 2000.  It seems obvious that if a variable exists containing a date value, then one could simply reset the variable to the millennium.  But, if you needed to know the difference between the date string and that event, that presents a more interesting challenge.  You could use PHP’s date(), but I recommend the DateTime object as follows:


<?php
define("EVENT_YEAR",2000);
function getInterval( $strDate ){
list( $year,,) = explode( '-', $strDate );
return ($year - EVENT_YEAR);
}
$strDate = '2017-05-05';
$interval = getInterval( $strDate );
try {
$date = new DateTime( "$strDate -$interval years" );
} catch (Exception $e) {
echo $e->getMessage();
 exit(1);
}
if ( $date->format("Y-m-d") == '2000-05-05') {
    echo "\nThe interval is $interval years.";
}

See demo
If you wish to know more about the DateTime object, you may wish to read this article.

Now, let’s switch to the front-end and look at the following JavaScript which when posted originally appeared, as follows:

msg = 'igloomotor';

for (var x = 0; x < msg.length - 1; x++) {
    if (msg.length == 1) {
        for (var i = 0; i < 4; i++) {
console.log(i);
}
}
else   {
for (var i = msg.length; i > (msg.length - 1); i++) {
            console.log(i);
        }
    }
}

It would be helpful to know what the output should be in order to determine what’s wrong with the code.  In tinkering with it,  I concluded that the output should be the numbers one to ten in descending order.  So, here’s one way to achieve that outcome:

var msg = 'igloomotor';
for (var x = 0; x < (msg.length - 1); x++) {
    if (msg.length == 1) {
        for (var i = 0; i < 4; i++) {
console.log(i);
}
}
else     {
for (var i = msg.length; i > (msg.length - 1); i--) {
            console.log(i);
            msg = 'i';
        }
    }
}

This code decrements variable i in the second for-loop.  I also changed the length of msg by altering its value in the second for-loop.  Cute exercise!

Of note, the if-conditional never evaluates as true. The loop in the ‘else’ executes and displays the original length of msg which was assigned to i. As i gets decremented its decreasing value is outputted. After the loop finishes the outer loop fails because the length of msg is now 1, as is the value of x resulting in a test of whether ‘1 < (1 – 1)’ which must evaluate as false, and so the  script ends.

Hope you enjoyed these technical tidbits.

This work is licensed under a Creative Commons License

Advertisements

Actions

Information

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s




%d bloggers like this: