Blog

PHP – Foreach By Reference

Today I was writing some code in which I wanted to modify the values of an array within a for-loop. The obvious solution would be to use the key to modify the value but there is actually another way of doing it: The above code will actually go through the Read more…

By Chris West, ago
Blog

JavaScript – Parsing A Number

If you should ever need to parse a number into an array of bits or a different base you can use the following function: var splitNumber = (function(MAX) {   for (; MAX + 1 – MAX == 1; MAX *= 2){}   return function (num, radix) {     // Validate num     num Read more…

By Chris West, ago
Blog

EcmaScript 6 – Array.of()

A function that is proposed to be added to EcmaScript 6 is Array.of(). This function will be used to create an array of the passed parameters. Since this function is available in some browsers but not in all, here is a block of code which when executed will define it Read more…

By Chris West, ago
Blog

JavaScript – Range Array Function

A cool function that exists in Python is the range function which actually creates a list of numbers within the specified range. Once again, since I love JavaScript, here a quick imitation of this function: Of course, this is probably not a complete imitation, but as you can see it Read more…

By Chris West, ago
Blog

JavaScript – Better Array Builder

Yesterday I wrote this post about a way to loosely imitate having array comprehensions in JavaScript. After thinking about it a bit more, I thought it would be best to change the function to be more like this: I wanted to remove the declaration of a variable which will be Read more…

By Chris West, ago
Blog

JavaScript – Weighted Randomization

Today I worked on a project which involved weighted randomization. In other words, I had to randomly choose an item from an array of weighted items. The items that were assigned a higher weight were to be more likely to be picked. The following is a JavaScript function that accomplishes Read more…

By Chris West, ago