Day 10, N queens

Today, we started working on N queens problem. I don’t even know the rules of rooks or queens, so when I was asked to answer a question from Marcus, I totally bluffed.

Our toy problem is an interesting one. In short, you are supposed to use Math.random to write a random1to5 function that returns a random integer of 1 to 5. Then, you are supposed to use random1to5 to write a random1to7 function that returns a random integer of 1 to 7. My initial thought was fairly naive because I forgot about the integer requirement. I scaled the number space between 1 to 5 to the number space between 1 to 7. That was easy, I thought. When I started writing the test, I realized I answered the wrong question. The real question is similar to: given a dice, how to generate a random integer between 1 and 7. My short answer is that you just need to throw the dice 7 times.

We also learned about jQuery. It is ridiculous that I have been using jQuery without understanding the difference between the jQuery collection object and the DOM objects.

What is jQuery?

The short answer is jQuery is a function that produces jQuery collection object.

What is a jQuery collection object?

– It is an array-like container.

– It contains zero or more DOM nodes.

– It also contains tons of jQuery methods, which are functions that attach to the jQuery collection object and allows easy interaction with the wrapped DOM nodes.

– $node[index] will return a DOM node WITHOUT any jQuery methods.

Three ways to generate a jQuery object:

1. jQuery(‘<p>dd</p>’);

2. jQuery(‘.name’);

3.jQuery(aDOMNode);

4. Note: jQuery collections don’t update dynamically. (I miss you, Meteor)

What can jQuery methods do?

– CRUD + more. Create; read such as $node.parent(), $node.each(callback); update such as $node.animate(), $node.on(‘click’, callback), $node.appendTo(target); delete;

Other lessons:

– Elements panel in Chrome is the actual DOM object represented in HTML. That is why they are perfectly indented.

– Any object has a splice() method will be treated by Chrome console as an array like object.

Advertisements
Standard