Claus Witt

software and web developer

Archive for the ‘javascript’ tag

Project Euler Problem Five

without comments

Continuing our combined node.js and project euler posts. We got to problem five, which asks us “What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?”.

First of all we only check numbers below or equal to 20! because we know for sure that this number will satisfy the criterium of the question, we just don’t know if there is a result below this number, we also know that the number must be at least 20. For this reason we check all numbers between 20 and 20! if all numbers between 1 and 20 is a divisor – if one of the numbers fail, we move on to the next candidate. As soon as a candidate proves to be correct, we exit and return the result.

Written by Claus Witt

June 7th, 2010 at 7:30 am

Project Euler Problem Four

without comments

Find the largest palindrome made from the product of two 3-digit numbers. This is problem four of project euler, and this one actually caused me some problems. The reason was that I wrongly assumed that a number with the largest divisor would also would be the largest number. But of course this is not necessarily the case.

As the previous three problems I have posted about, this is written to run on node.js.

Written by Claus Witt

June 4th, 2010 at 7:00 am

Project Euler Problem Three

without comments

Continuing my posts about Project Euler Problems, this time with problem three, which asks us to find the “the largest prime factor of the number 600851475143″.

This time I have decided to explain the code a bit more, through comments – since this problem is a bit more tricky than the previous two problems.

Once again, you will need node.js to run the code.

Written by Claus Witt

June 1st, 2010 at 7:00 am

Project Euler Problem One

with 2 comments

A long time ago I decided to do a couple of posts about project euler. I find the problems to be great excersize. It was not before I began to look into node.js though, that I began writing code for the problems for the purpose of publishing it.

The first problem on the list is very simple. “Find the sum of all the multiples of 3 or 5 below 1000″. The solution, then, must be to find all numbers that are multiples of 3 and 5, and then adding those…

This solution is pretty simple. You (probably) need nodejs to run the code.

Written by Claus Witt

May 29th, 2010 at 7:55 pm

Posted in Development

Tagged with , ,

New Versions of Dojo and Plugd Released

without comments

I have not posted for about a week now, the main reason is that I am working on something very exciting, which will be features here in the comming days, if all works out.

Today, while reading the most recent posts on Ajaxian, I found the announcement of the Dojo 1.3 release. Read the announcement and learn about the plugd plugin.

I will be using some time to update our project, which by the way will be going into the first testphase in 13 days, to the newest version of Dojo Toolkit today.

Written by Claus Witt

April 1st, 2009 at 7:16 am

Posted in Web development

Tagged with , ,

Upload Progress Bar using Php and Dojo

without comments

For a long time I have wanted my applications to have progress bars on uploads, but I have always thought that this was impossible when the server language was php. Unless of course you wanted to do some mixing of languages, and handle the upload via perl. I, however, wanted complete integration of the code, and wanted my mvc framework to be able to output the wanted information. A while ago my boss at Voicearchive asked me if we could get a progress bar for uploads on the system that we are developing. Once again I found myself searching for possible solutions.

This time around, however, I found what I was looking for. I found an article about PHP and jQuery upload progress bar. In this article I learned about the PECL extension uploadprogress. I began trying to make this solution work.

First thing, was that I found the extension did not work as I wanted. After a while I found that installing newest version, required installing it via “pecl install channel://pecl.php.net/uploadprogress-0.9.2″ – which gave me the latest beta release of the extension. Now it returned the correct information when I uploaded a file.

The solution consists of 5 parts.

  1. The Pecl extension
  2. The form
  3. The progressbar widget
  4. The javascript
  5. and the controller which sends the data from uploadprogress as json to the client

After the installation was managed, I changed the form to have the required hidden UPLOAD_IDENTIFIER value included.


The value of the field is set when the dialogbox containing the upload form is revealed, and saved in a variable, that can be used when the script needs to poll for the result.

lastFileProgressKey = guid();
console.log(lastFileProgressKey);
dojo.byId('addFileProgressKey').value = lastFileProgressKey;
dijit.byId('addFileDialog').show();

The guid function is just a simple function which generates a guid-like random string. The value is then set to the form, and the dialog is revealed.

When the upload button on the form is pressed it calls a event handler which handles the actual upload. Before the upload is started however the following function is called.

startUploadCheck(lastFileProgressKey);

These function do the actual work.

function updateProgressBar(response) {
	if(response) {
		totalBytes = parseInt(response.bytes_total);
		uploadedBytes = parseInt(response.bytes_uploaded);
		percentage = Math.floor(100 * uploadedBytes/ totalBytes);
		uploadProgressBar.update({ progress: percentage+'%'});
		if(lastUploadId!='') {
			setTimeout("checkUpload()", 1000);
		}
	} else {
		if(lastUploadId!='') {
			setTimeout("checkUpload()", 1000);
		}
	}
}

function checkUpload() {
	dojo.xhrGet( {
		url :'/uploadprogress/check?uploadid='+lastUploadId,
		handleAs: 'json',
		preventCache: true,
		load: updateProgressBar,
		error: function(data) {
			if(lastUploadId!='') {
				setTimeout("checkUpload()", 1000);
			}
		}
	});
}

function startUploadCheck(uploadid) {
	dijit.byId('uploadProgressDialog').show();
	lastUploadId = uploadid;
	checkUpload( uploadid );
}

function hideProgressDialog() {
	lastUploadId = '';
	dijit.byId('uploadProgressDialog').hide();
}

The dojo widget used is the ProgressBar.

The controller is the simplest part of the equation.

public function check() {
    $formUploadId = $this->request['uploadid'];
    return json_encode(uploadprogress_get_info($formUploadId));
}

All the legwork is done by uploadprogress_get_info, which then is passed through json_encode.

Written by Claus Witt

February 25th, 2009 at 1:00 pm

Posted in Development,Web development

Tagged with , ,