Blog

JavaScript – Euclidean Algorithm

One of the things that you often have to learn early on in school after multiplication and division is how to find the GCD (Greatest Common Divisor) of two numbers. It is possible that you know the GCD as the GCF (Greatest Common Factor) or the HCF (Higest Common Factor). Read more…

By Chris West, ago
Blog

POW Answer – Explain That SQL #1

To answer last week’s POW, the purpose of the SQL was to generate a random string of letters and numbers. I actually wrote two different JavaScript function that can produce the same results. The following is the slightly more straight-forward solution: function randomChars(len) { for(var n, s = ""; --len;) Read more…

By Chris West, ago
Blog

JScript – Recursively Call A Script

Have you ever wondered how you can recursively call a JScript? Here is an example which shows how it can be done: WshShell = new ActiveXObject("WScript.Shell"); switch(WshShell.Popup("Do you want to run this script again?", 10, "Run Script Again", 4 + 32)) { case -1: WScript.Echo("I guess you aren't there. See'ya!!!"); Read more…

By Chris West, ago
Blog

JavaScript – Binary Search

The following is an implementation of a binary search for arrays which gives you the ability to do fuzzy matches (default), exact matches, or specify your own comparison function: (function() { // Use this if the search isn't called with a 2nd param or it // is false. function fnFuzzy(a, Read more…

By Chris West, ago
JavaScript

POW Answer – Unnamed Function #1

The answer to last week’s Problem of the Week is that the following definitions represent the factorial of the given non-negative integer: Mathematical Definition Function Definition Condition unnamed(n) = 1 if n = 0 n × unnamed(n – 1) if n > 0 JavaScript Definition function unnamed(n) { if(n == Read more…

By Chris West, ago
Blog

Forcing A Constructor In JavaScript

Did you know that you can force a function to only act as a constructor? The following is an example of a Person pseudo-class which can be defined with or without the new keyword: // Defines a person object. function Person(firstName, lastName, age) { // If called without parameters. if(!arguments.length) Read more…

By Chris West, ago
Blog

Deep Copy of Arrays

With jPaq and many other JavaScript libraries, it is very easy to do a shallow copy of an array. On the other hand, it isn’t as easy to do a deep copy of arrays. For this reason, I plan on replacing the Array.prototype.clone() function with the following: Array.prototype.clone = function(doDeepCopy) Read more…

By Chris West, ago