This is a weekly update of some interesting things relating to JavaScript, to help encourage discussion and to bring some potentially new ideas to the fore.

Here's what we've seen of interest this week.

Quiz

You Can't JavaScript Under Pressure - Five functions to fill. One ticking clock. How fast can you code? If I'm completely honest, I needed 17 minutes and five seconds. Who can beat that? (shouldn't be hard :)).

Free Book

JavaScript Allongé by Reginald Braithwaite is a free book (to read online) that focuses on using functions to write simpler JavaScript with fewer bugs

Listening

Chales Max Wood and Joe Eames talk about working from home on the most recent JavaScript Jabber podcast.Does anyone reading this work from home? I do, and I love it!

Take a look at these recent happenings in JavaScript, let us know what you think about them, and we'll have some more to inspire you next week.

Feel free to [PM me or [URL="http://www.sitepoint.com/forums/private.php?do=newpm&u=158377"]PM Paul](http://www.sitepoint.com/forums/private.php?do=newpm&u=184222) if you have anything interesting for the next issue.

daveporter
—
2013-10-07T03:19:19Z —
#2

Oops! The Free book does not appear to be free (unless you want to read it on-line!)Cheers, Dave

Pullo
—
2013-10-07T06:49:22Z —
#3

daveporter said:

Oops! The Free book does not appear to be free (unless you want to read it on-line!)

Thanks for pointing that out, Dave. I amended my post accordingly to reflect that fact.It's still a pretty good deal, though

Well, not being a programmer doesn't help ... and I hate the timer thing, but it's still fun to have a go and learn something. Pullo has also been coaching me along on the sly, too. I'm up to question 4.

Pullo
—
2013-10-08T11:30:28Z —
#6

paul_wilkins said:

Did you (like me) make a couple of runs at it?

Only because in a fit of clumsiness I refreshed my browser half-way through. DOH!

For me the main sticking point was question 5.

I'm still helping Ralph with one of these questions (so don't want to give the answer away), but once I've heard back from him maybe we can compare answers / techniques.

PaulOB
—
2013-10-08T14:09:37Z —
#7

Hehe, I'm still going. It's up to about 56 hours now.

I had a go and managed the first 4 (after a bit of googling) but stuck in a recursive loop on question 5 at the moment.:)

PaulOB
—
2013-10-08T15:28:57Z —
#8

Pullo said:

I needed 17 minutes and five seconds. Who can beat that? (shouldn't be hard :)).

It took me " 178 minutes, 37 seconds for all 5 levels. Well done! "

But that did include walking the dog, having lunch, a little bit of work and vast amounts of googling

I'm not sure my last answer was good coding though so if someone wants to tidy it up and point out the errors it would be appreciated.

[spoiler]//test5var total=0;function arraySum(i) {

// i will be an array, containing integers, strings and/or arrays like itself.
// Sum all the integers you find, anywhere in the nest of arrays.
for (var loop= 0; loop &lt; i.length; loop++)
{
// check if it is an array
if (i[loop] instanceof Array) {
var nested = i[loop];
arraySum(nested);
}
// check if a number
if (typeof i[loop] == "number"){
total= total + i[loop];
}

} return total;}

[/spoiler]

Pullo
—
2013-10-08T15:43:04Z —
#9

Hi Paul,

That's exactly how I did it, except that you can eliminate the need for the global variable like so:

I ended up having syntax issues when using typeof and blanked on instanceof, so a different solution was used by me.Now that I know that they do work, I'll take another run at things later.

Pullo
—
2013-10-08T18:59:31Z —
#12

So, the first two are no brainers:

function doubleInteger(i) {
// i will be an integer. Double it and return it.
return i*2;
}

function isNumberEven(i) {
// i will be an integer. Return true if it's even, and false if it isn't.
return i%2 === 0;
}

With three I still had the "under pressure" bit in my mind, so I just went with what I knew:

function getFileExtension(i) {
// i will be a string, but it may not have a file extension.
// return the file extension (with no period) if it has one, otherwise false
var regEx = /.*\\.(.+)/;
if (i.match(regEx, "")){
return i.replace(regEx, "");
} else {
return false;
}
}

Happy to see you've switched from equality == to identity === in your latest examples. We could be comparing to "0" using equality in @Paul_O_B; #2, for example, and due to type conversion, it will still work, but it's not advisable.

A suggestion for #3:

function getFileExtension(i) {
// i will be a string, but it may not have a file extension.
// return the file extension (with no period) if it has one, otherwise false
var extension = i.split(".").pop();
return ( extension !== i ) ? extension : false;
}

Here I'm caching the result of string splitting, and get the last element, using Array.prototype.pop(). If the last (and only) element is the string itself, there was no splitting involved, so we return false.

PaulOB
—
2013-10-08T20:11:45Z —
#14

myty said:

@Pullo; We could be comparing to "0" using equality in @Paul_O_B; #2, for example, and due to type conversion, it will still work, but it's not advisable.

Ok thanks

From reading a little it seems that its safer to always use "===" rather than "==".

For #4 and #5 actually functional programming design patterns is what comes to mind.

4

@Pullo; uses Array filter, one of the new additions to ecmascript. Unfortunately, it's not quite mainstream, but the underscore.js library is a good use here.

We first reject from the array the non-string elements (first filter), and then max the longest string out (second filter): http://jsbin.com/OkOGIxI/2/editHide HTML and Output, show Console and hit run.

5

Functional programming design patterns bring clarity, and once the new Array methods become mainstream (once IE8 goes extinct), we can drop underscore.js. Until then, I'm glad this little gem of a library exists.

system
—
2013-10-08T20:49:13Z —
#16

Pullo said:

What got me here is that I was expecting to be able to test for an array with typeof i[j] === "array" which didn't work.