JavaScript code answer

if (x!=x) console.log("Wahay!");

A while back I posed a little JavaScript question: When would the above JavaScript statement print “Wahay"?

This provoked a storm of apathy on the internet, but I’m going to tell you the answer anyway. You use the above test to detect a variable which is not a number (NaN).

JavaScript is a bit strange when it comes to handling errors.

x =  1/"fred";

Some languages would not even compile the above statement. Some languages would stop the program at this statement. JavaScript does something different. It says “Dividing a number by a string is silly, I’m going to set the value of x to a special value which means “Not a number”. JavaScript has other special values too. If you divide any number by 0 you get a special value called Infinity, which behaves like infinity. Add a number to infinity and you get infinity as a result. Divide any number by infinity and you get zero. Divide infinity by infiniuty and you get Not a Number - which is also right.

But I digress. What would the statement at the top do? Well, it is how you test for Not a Number in a JavaScript program. Suppose you want to check if x contains NaN. You might think that you do this:

if (x==NaN) console.log("Not a number!");

However, this statement will not work, because something which is not a number is not equal to anything, including NaN. So the test always fails. Which is why you sometimes see NaN in JavaScript forms because the programmer didn’t know this or test it.

However, a value which is not a number isn’t even equal to itself, which is why the test is so useful.

Chocolate Synthbox at the next Hardware Meetup

The next Connected Humber Hardware Meetup is on Wednesday 3rd of August at Hull MakerSpace, starting at around 5:30. I’m taking my Chocolate Synthbox, which I’m hoping will be a bit more reliable than last time I showed it off. If you want to have a play with it, or bring something musical along of your own, we’d love to see you Find out more here.

I sat on my laptop

A while back I bought a super-cheap laptop, just to see what you can get for 59 quid. It turns out that it runs Windows 11 - its the only machine I’ve got that can. I’ve been using it to make Windows 11 screenshots. Anyhoo, long story short, the laptop got left on a chair and I sat on it. The first thought was that I wondered why the chair was suddenly so uncomfortable. Then I heard the tiniest of creaks and I realised what I’d done.

The laptop was absolutely fine though. I’m really impressed. I’m not planning on sitting on it again, but it is nice to know that it can handle things like this. It’s still on sale too. You can find it here.

Great Birthday Fun

Had a fantastic birthday today. We headed up town for a coffee and then took a walk around Hull Museum Quarter. The weather was excellent and the wonderful transport museum had even laid on a special exhibition of classic cars for us to look at. Then on to Berts for lunch. After that it was back home for cake and and fun and games. We spent the evening playing the excellent Point Salad and DownForce games. Great stuff.

Wrapping temperature sensors

One of the things we discussed last night at our Meetup was the our air quality sensors. We started out with four sensor three years ago, in locations around Hull. All the ones still in place are still working, but they have fallen foul of the recent heatwave.

The temperature sensors are wrapping round when the temperature gets above 40 degrees. To save space in each LoRa message the temperature data is restricted to a range of -24 to +40. This gives a range of 64 which can be expressed in six bits. As you can see above, this doesn’t work too well when things get really toasty.

I think we could use some cunning code to figure out what that temperature should be, even though it has wrapped around. However, the next version of the code is will need to be changed to handle these high temperatures that we are now seeing.

Dirtywave M8

I’ve got a new sound device to fixate over. It’s the Dirtywave M8. It’s a bit like the old school trackers that we used to run on the Amiga all those years ago. It’s beautifully made, sounds awesome and you can’t buy it anywhere.

But, you can run the firmware on a Teensy 4.1 and connect this to your computer to get the user interface and sound output going. I’ve ordered a Teensy 4.1 (which are also pretty hard to track down). Quite excited.

Fan hunt

One of my biggest fans……

There’s a very popular program on the BBC called “The Repair Shop”. In it a bunch of highly skilled crafts folk mend the heirlooms of families that have fallen into disrepair (that’s the heirlooms - not the families). I felt a bit like one of the crafts folk as I surveyed the collection of fans that I’d just got down from the loft in preparation for the “Deadly Heat Wave” (tm) that is coming.

All the devices were a bit grubby. I took them to pieces, cleaned them up and put them back together. I also checked the mains plugs and re-seated the cord grip on one. At the end I had three fans that were good to go (although the bearings on one have to be heard to be believed).

Bring on the heat….

Found it!

“|t is a truth universally acknowledged that the best way to find a missing cable is to buy a replacement”.

Yesterday I lost a cable. So I bought a replacement. Today I thought I’d take a look at the original cable order. It turns out that I’d forgotten what the cable looked like, so for the last couple of days I’ve been searching for the wrong thing. A quick scan of the desktop and there it was. Oh well.

Staying sane with missing cables

One trick to stay sane while writing a book is to fixate on something completely unimportant at the same time. One part of my brain is trying to explain JavaScript while another part is frantically trying to remember where I put the USB 2.0 Type A Female to USB B Male cable that I use to connect my Synthstrom Deluge to usb MIDI devices. I have absolutely no need to connect my Deluge to a MIDI keyboard at the moment, but that’s not the point. I’m getting regular exercise breaks too as I get up and look in places that the cable might be.

By mid afternoon I snapped and ordered an adapter which does the same job as the cable. Cost: two pounds fifty pence. Of course, now I’ll have to find something else to worry about.

The writing itself is going fine, which is the important thing.

Nerina Pallot is at Cottingham Folk Festival

I’ve been listening to Nerina Pallot songs ever since I heard one of the kids playing her music and copied their CD onto my computer. And now she is appearing at Cottingham Folk Festival at the end of August. If you like melodic stuff with piano and guitar plus the odd rocky number to prove that she can do that too, then you should go along, like I’m doing. There are lots of other interesting looking acts too, including Judy Collins and New Model Army.

Enter the Glossary

If you’re not sure what a glossary is you can always look it up. Pause for laughter.

Anyhoo, I’ve decided to add a Glossary to my new “Begin to Code” book. Unlike earlier books, this one assumes a level of previous programming knowledge. That’s fine, but what if you know most of it but are missing a few bits and bobs? That’s where the glossary fits in. The idea is that you if you find something you’re not clear on you can drop into the glossary, read up on the thing you need help with and then head back to the text. I suppose you could try starting with no programming knowledge, read the entire glossary and then start on the text, but I’m not sure if this would end well.

I’ll be putting out some sample pages soon.

Single paragraph science fiction: 1

The captain looked out of the porthole at the antenna. One hundred miles wide, glinting in the distant sunlight. It had taken five years to build it in earth orbit. Then another five years to fly it to this lonely spot on the edge of the solar system. And all of it a race against time before the signal became too faint to detect. The bridge computer beeped and announced “Ten seconds to transmission”. The captain sat upright. “Those calculations had better be right” she thought to herself. It had taken the finest computers on earth to plot the trajectory of the signal on it’s 200 year journey, bouncing off planets from the transmitter to this precise location. She looked at the screen. Nothing but static so far. Then the screen cleared and displayed a familiar logo. A tune she had known since childhood blasted out of the speakers and the titles appeared. The last missing “Dad’s Army” episode had been found….