Category Archives: software

Code of the Day: Javascript, Prime Factors of a Number

Javascript: Prime Factorization

Today’s code is a enhancement of Code Renaissance‘s version of “Finding Prime Numbers in Javascript”.
Main difference are the following.

  1. Faster performance by eliminating recursion and caching Math.sqrt.
  2. Whole numbers bigger than 1 return an empty array, since they’re not prime numbers.
  3. Decimal values are converted to whole numbers.

/**
* @author Larry Battle - http://bateru.com/news/contact-me
* @date May 11, 2012
* @license MIT and GPL v3
* @purpose Return the prime factors of a number.
* @info - http://bateru.com/news/?s=prime+factors
*/
var getPrimeFactors = function(num) {
    num = Math.floor(num);
    var root, factors = [], x, sqrt = Math.sqrt, doLoop = 1 < num;
    while( doLoop ){
        root = sqrt(num);
        x = 2;
        if (num % x) {
            x = 3;
            while ((num % x) && ((x += 2) < root));
        }
        x = (x > root) ? num : x;
        factors.push(x);
        doLoop = ( x != num );
        num /= x;
    }
    return factors;
}

Example:
getPrimeFactors(15120) // returns [2, 2, 2, 3, 3, 3, 7]

Demo:

Video of 5 Autonomous Robots

New Honda Robot ASIMO 2012 – All features and behaviors

BigDog Evolution

A Swarm of Nano Quadrotors

Freaky Life Like A.I Robot

Awesome Robot For Kids, 15 inches High

4 Notepad++ Plugins for Javascript Developers

Here are some Notepad++ plugins for JavaScript Developers that I found useful.

Tip:
You can install all the plugins using Notepad++ Plugins Manager located in the “Plugins” Menu.

  1. JSLint


    Description:
    JSLint is a static code analysis tool used in software development for checking if JavaScript source code complies with coding rules.


    Tip:
    If jslint turns out to be strict for you, then I recommend that you try jshint.com.
    Does it make any sense to use JSLint and follow it?

  2. JSMin


    Description:
    JSMin is a filter which removes comments and unnecessary whitespace from JavaScript files.


    Tip:
    The great thing about this plugin is that it formats and minimizes.
    Link:
    JavaScript Compressor and Comparison Utility.

  3. JSON Viewer


    Description:
    A JSON viewer plugin for Notepad++. Displays the selected JSON string in a tree view.


    Tip:
    If you get “Could not parse!!” error message, then reformat your code with JSON.stringify().

  4. RegEx Helper


    Description:
    A Notepad++ plugin that allows users to develop regular expressions and test them against their open documents.

Code of the day: Fix for Number.prototype.toFixed

In javascript, Number.prototype.toFixed has problems rounding.
Here’s the fix and some test cases.

Number.prototype.toFixed = (function () {
	var oldToFixed = Number.prototype.toFixed;
	return function (precision) {
		var value = this.toString(), power = Math.pow(10, precision || 0);
		return oldToFixed.call((Math.round(value * power) / power), precision);
	};
}());


Here’s a simple test.

// Programmer: Larry Battle
// Link: http://bateru.com/news/2012/03/code-of-the-day-fix-for-number-prototype-tofixed
(function (root) {
	"use strict";
	var log = function( str ){
		root.document.body.innerHTML += "log: " + str + "<br/>";
	},
	assert = function (a, b ) {
		var message = a + ' !== ' + b;
		message += ( a === b ) ? "=> true" : "=> false";
		log( message );
	};
	assert((0.595).toFixed(2), "0.59");
	assert((0.9).toFixed(0), "1");
	Number.prototype.toFixed = (function () {
		var oldToFixed = Number.prototype.toFixed;
		return function (precision) {
			var value = this.toString(), power = Math.pow(10, precision || 0);
			return oldToFixed.call((Math.round(value * power) / power), precision);
		};
	}
		());
	log("Applying the toFixed fix.");
	assert((0.595).toFixed(2), "0.60");
	assert((0.9).toFixed(0), "1");
}(this));

Demo: Link

Update!
Improved code here

Bad Behavior has blocked 1087 access attempts in the last 7 days.

FireStats icon Powered by FireStats