JavaScript Map()

Coming from a (mostly) non-computer science degree background, I’m very much aware that I have some blindspots that need to be addressed.

The other things I bring to the table as a result of a decade and a half as a professional mean that I’m still able to create a lot of value for my organization, but the more I know, the better I’ll be able to contribute.

I’ve actively been working to address those blind spots, and one of the wonderful things that I’ve learned or re-learned along the way is hash tables.

Hash tables are great because a well-designed one lets you find a key and pull out the value in O(1) complexity.

JavaScript’s implementation of hash tables is the Map object. There are a lot of algorithms where a map function is hugely helpful, including the rendevous algorithm that I’ve been working on for the last week or so.

Tonight, after spending a decent amount of time debugging a number of issues with my code so far, I realized that next issue I was up against was because I thought I was saving number keys in my map rather than string keys.

map.get() and map.delete() don’t do any kind of type coercion, so you have to be very careful that you’re comparing apples to apples between the item you’re looking for and the keys in your map.

Lesson learned!

ServiceNow Fix Scripts

When I was first starting out with ServiceNow, someone pointed me to the scheduled jobs when I needed to run a script on a recurring basis, and I’ve ended up using on demand scheduled jobs to test pieces of script includes I was creating.

For the most part it’s a great way to run a script on demand, but a while ago I found out that you can use fix scripts in ServiceNow to run code, and they have the astounding benefit of letting you roll back the changes from your script if things don’t go as you were expecting them to.

The next time you need to move data around using a script inside of ServiceNow, use a fix script!

Hat tip to my co-worker Kim for teaching me that.

ServiceNow creating an event from inside a script

I’m so far behind with posting blog ideas that a lot of times when I go back to my list the things that I was excited to blog about previously now seem trivial.

However trivial they may be though, there is likely someone else out there that doesn’t know them yet, so I’ll continue posting them in the hopes that someone can save half an hour debugging because they stumbled across my blog while doing a google search on the topic.

It’s possible (and easy) to create a event from inside of a script. You use gs.eventQueue()

Then you pass the name of the event, a Glide Record object, and the values for the parm 1 and parm 2 on the event.

The tricky part is that my normal method for getting a Glide Record doesn’t work. I normally do something like this:

var gr = new GlideRecord(‘sys_user’);

gr.addQuery(‘sys_id’, ”d3489ac7dbbe8c10c772f9baae9619e8′);

gr.query();

gr.next();

However, passing gr into my gs.eventQueue() call didn’t work. Instead, apparently, you have to use the gr.get() construction.

For instance:

var gr = new GlideRecord(‘sc_req_item’);

gr.get(‘d3489ac7dbbe8c10c77af9baae9619e7’);

gs.eventQueue(‘my.event’, gr, ‘869104dc1b7c7b48ubbd43b3cd4bcb53’, ‘f2b1694edb768010co72f9baae9619c3’ );

 

A Really Good Learning Resource

More than a year ago, I had someone recommend that I check out the video archives from Berkeley’s 2015 computer science 186 class.

I’ve spent roughly half of my free time since then doing ServiceNow consulting, but that seems to be winding down a little now, so I went out and watched the first class.

I quite enjoyed it, and have spent most of the last week since I watched the first class working through what I imagine the assignment for the class might have looked like.

I’ll post more about that once I have my algorithm working, but in the mean time, here is the link to the series:

https://archive.org/details/ucberkeley-webcast-PL-XXv-cvA_iBVK2QzAV-R7NMA1ZkaiR2y?sort=titleSorter

Hacker Rank Ransom Note Problem

Here’s my solution to the Ransom Note problem found here:
https://www.hackerrank.com/challenges/ctci-ransom-note/problem

The title explicitly points out that it should be solved with a hash table. My logic is in the isViableCombination function. The checkMagazine function just formats the output the way that the test is expecting to receive it (with console.logs).

function checkMagazine(magazine, note) {
   if(isViableCombination(magazine, note)) {
      console.log(‘Yes’);
   } else {
      console.log(‘No’);
   }
}
function isViableCombination(magazine, note) {
   if(note.length > magazine.length) {
      return false;
   }
   let words = new Map();
   for(leti=0;i<magazine.length;i++) {
      let value = words.get(magazine[i])
      if(!value) {
         words.set(magazine[i], 1);
      } else {
         words.set(magazine[i],value + 1);
      }
   }
   for(letj=0;j<note.length;j++){
      let value = words.get(note[j]);
      if(!value) {
         return false;
      } else {
         words.set(note[j],value – 1);
      }
   }
   return true;
}
If the magazine has fewer words in it than the ransom note, then you know off the bat that it won’t work for that particular ransom note.
Complexity is O(m + n). (If m is the length of the magazine and n is the length of the ransom note.)