Day 68: Back to Pretty Printing A Tree – LOL

I got through the basics of C type declarations and working with characters and strings. I hope that’s enough to make an impact so it’s time to try my hand again at finishing my horizontal pretty print function for a tree data structure. This should be fun. I already started re-organizing my code now that I know how to use function prototypes. I hope my effort shines through. Fingers crossed!

TLDR;

Okay, so here are the highlights of what I did:

  • Continued to work on the “Technical Questions” section of the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. Within that, I continued my work on Tree data structures. Started trying to replicate the example pretty print functions provided in the article I am reading through. I got back to working on the Horizontal Pretty Print function. I don’t know if I covered enough of the language but I feel a bit more comfortable and would like to try to see if I can get to the finish line this time.
  • I continued reading through chapter 6 of the book “Eloquent JavaScript” by Marjin Haverbeke. I am starting to read the book to help cover some insecurities I have about my knowledge of the language. I learned JavaScript on the fly through projects and YouTube videos. There are some core questions about the language that I don’t think I can answer, so hopefully this will help. I was finally able to move forward in the chapter to cover, “overriding derived properties” and the introduction to “Polymorphism” in JavaScript. I got stuck at polymorphism since I never fully studied the concept in depth. The book only briefly mentions it and it confused me so I may need to spend a bit more time here. The topic is covered in Chapter 6 after the introductions of classes and such. Symbols were also mentioned which were even more confusing LOL.

JavaScript Notes

Overriding Derived Properties

When you add a property to an object, whether it is present in the prototype or not, the property is added to the object itself. If there was already a property with the same name in the prototype, this property will no longer affect the object, as it is now hidden behind the object’s own property. Basically, the object’s own properties take priority over the properties of the prototype. If it exists in the object itself there is no need to go to the prototype.

Rabbit.prototype.teeth = "small";
console.log(killerRabbit.teeth);
// → small

killerRabbit.teeth = "long, sharp, and bloody";
console.log(killerRabbit.teeth);
// → long, sharp, and bloody

console.log(blackRabbit.teeth);
// → small

console.log(Rabbit.prototype.teeth);
// → small

Overriding is also used to give the standard function and array prototypes a different toString method than the basic object prototype:

console.log(Array.prototype.toString ==
            Object.prototype.toString);
// → false

console.log([1, 2].toString());
// → 1,2

console.log(blackRabbit.toString());
// → '[object Object]'

console.log(Object.prototype.toString.call([1, 2]));
// → [object Array]

Maps

A map (noun) is a data structure that associates values (the keys) with other values. This is the core meaning of the term whereas it’s use in JavaScript’s Array.prototype.map() function the concept is applied to map adjusted/derived values for each “key” value that already exists in the array. In this case we are simply speaking about the data structure itself. For example, you might want to map names to ages. It is possible to use objects for this.

let ages = {
  Boris: 39,
  Liang: 22,
  Júlia: 62
};

console.log(`Júlia is ${ages["Júlia"]}`);
// → Júlia is 62

console.log("Is Jack's age known?", "Jack" in ages);
// → Is Jack's age known? false

console.log("Is toString's age known?", "toString" in ages);
// → Is toString's age known? true

NOTE: All object property names must be strings. This is something to consider when trying to construct a map data structure using a plain JavaScript object.

Due to the nature of prototypes and object properties, we have to be cautious on how we implement maps. JavaScript provides a few workarounds for this bug where the unintended property that exists on the prototype was accessed when it shouldn’t be.

  1. We can construct an object with no prototype using Object.create(null); and then it’s ready to use as a pure map data structure.
console.log("toString" in Object.create(null));
// → false
  1. We can use JavaScript’s standard Map class to construct a new instance of the class and use it’s features for a map data structure. If you need a map whose keys can’t easily be converted to strings e.g. objects, you cannot use an object as your map BUT you can use and instance of the Map class. It stores a mapping and allows any type of keys. The methods setget, and has are part of the interface of the Map object. Writing a data structure that can quickly update and search a large set of values isn’t easy, but we don’t have to worry about that. Someone else did it for us, and we can go through this simple interface to use their work. (Be sure to read up on docs to learn about all the built-in features.
let ages = new Map();

// Syntax for setting key-value pairs
// instance.set(key, value)

ages.set("Boris", 39);
ages.set("Liang", 22);
ages.set("Júlia", 62);

console.log(`Júlia is ${ages.get("Júlia")}`);
// → Júlia is 62

console.log("Is Jack's age known?", ages.has("Jack"));
// → Is Jack's age known? false

console.log(ages.has("toString"));
// → false
  1. If you do have a plain object that you need to treat as a map for some reason, it is useful to know that Object.keys returns only an object’s own keys, not those in the prototype. As an alternative to the in operator which includes an object’s prototype, you can use the hasOwnProperty method, which ignores the object’s prototype.
console.log({x: 1}.hasOwnProperty("x"));
// → true

console.log({x: 1}.hasOwnProperty("toString"));
// → false

Conclusion

That’s all for today. This is my sixth round of the “#100daysofcode” challenge. I will be continuing my work from round five into round six. I am currently working through the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. My goal is to become more familiar with algorithms and data structures. This goal was derived from my goal to better understand operating systems and key programs that I use in the terminal regularly e.g. Git. This goal was in term derived from my desire to better understand the fundamental tools used for coding outside of popular GUIs. This in turn was derived from my desire to be a better back-end developer.

I have no idea if my path is correct but I am walking down this road anyways. Worst case scenario I learn a whole bunch of stuff that will help me out on my own personal projects.