Showing posts with label Queens. Show all posts
Showing posts with label Queens. Show all posts

Thursday, November 15, 2018

ES6: Use class Syntax to Define a Constructor Function

ES6: Use class Syntax to Define a Constructor Function
ES6 provides a new syntax to help create objects, using the keyword class.

This is to be noted, that the class syntax is just a syntax, and not a full-fledged class based implementation of object oriented paradigm, unlike in languages like Java, or Python, or Ruby etc.

In ES5, we usually define a constructor function, and use the new keyword to instantiate an object.

var SpaceShuttle = function(targetPlanet){
  this.targetPlanet = targetPlanet;
}
var zeus = new SpaceShuttle('Jupiter');
The class syntax simply replaces the constructor function creation:

class SpaceShuttle {
  constructor(targetPlanet){
    this.targetPlanet = targetPlanet;
  }
}
const zeus = new SpaceShuttle('Jupiter');
Notice that the class keyword declares a new function, and a constructor was added, which would be invoked when new is called - to create a new object.


Use class keyword and write a proper constructor to create the Vegetable class.

The Vegetable lets you create a vegetable object, with a property name, to be passed to constructor.

Sunday, November 11, 2018

ES6: Write Concise Object Literal Declarations Using Simple Fields

ES6 adds some nice support for easily defining object literals.

Consider the following code:

const getMousePosition = (x, y) => ({
  x: x,
  y: y
});
getMousePosition is a simple function that returns an object containing two fields.

ES6 provides the syntactic sugar to eliminate the redundancy of having to write x: x. You can simply write x once, and it will be converted tox: x (or something equivalent) under the hood.

Here is the same function from above rewritten to use this new syntax:

const getMousePosition = (x, y) => ({ x, y });

Use simple fields with object literals to create and return a Person object.

Saturday, August 4, 2018

Iterate with JavaScript Do...While Loops

You can run the same code multiple times by using a loop.

The next type of loop you will learn is called a "do...while" loop because it first will "do" one pass of the code inside the loop no matter what, and then it runs "while" a specified condition is true and stops once that condition is no longer true. Let's look at an example.

var ourArray = [];
var i = 0;
do {
  ourArray.push(i);
  i++;
} while (i < 5);
This behaves just as you would expect with any other type of loop, and the resulting array will look like [0, 1, 2, 3, 4]. However, what makes the do...while different from other loops is how it behaves when the condition fails on the first check. Let's see this in action.

Here is a regular while loop that will run the code in the loop as long as i < 5.

var ourArray = [];
var i = 5;
while (i < 5) {
  ourArray.push(i);
  i++;
}
Notice that we initialize the value of i to be 5. When we execute the next line, we notice that i is not less than 5. So we do not execute the code inside the loop. The result is that ourArray will end up with nothing added to it, so it will still look like this [] when all the code in the example above finishes running.

Now, take a look at a do...while loop.

var ourArray = [];
var i = 5;
do {
  ourArray.push(i);
  i++;
} while (i < 5);
In this case, we initialize the value of i as 5, just like we did with the while loop. When we get to the next line, there is no check for the value of i, so we go to the code inside the curly braces and execute it. We will add one element to the array and increment i before we get to the condition check. Then, when we get to checking if i < 5 see that i is now 6, which fails the conditional check. So we exit the loop and are done. At the end of the above example, the value of ourArray is [5].

Essentially, a do...while loop ensures that the code inside the loop will run at least once.

Let's try getting a do...while loop to work by pushing values to an array.


Change the while loop in the code to a do...while loop so that the loop will push the number 10 to myArray, and i will be equal to 11 when your code finishes running.


Wednesday, July 25, 2018

Basic JavaScript: Using Objects for Lookups


Objects can be thought of as a key/value storage, like a dictionary. If you have tabular data, you can use an object to "lookup" values rather than a switch statement or an if/else chain. This is most useful when you know that your input data is limited to a certain range.

Here is an example of a simple reverse alphabet lookup:

var alpha = {
  1:"Z",
  2:"Y",
  3:"X",
  4:"W",
  ...
  24:"C",
  25:"B",
  26:"A"
};
alpha[2]; // "Y"
alpha[24]; // "C"

var value = 2;
alpha[value]; // "Y"

Convert the switch statement into an object called lookup. Use it to look up val and assign the associated string to the result variable.

Friday, September 15, 2017

#FreeCodeCamp Change Text with Click Events

#FreeCodeCamp Change Text with Click Events

When our click event happens, we can use jQuery to update an HTML element.
Let's make it so that when a user clicks the "Get Message" button, we change the text of the element with the class message to say "Here is the message".
We can do this by adding the following code within our click event:
$(".message").html("Here is the message");

Saturday, July 29, 2017

2Centz: Is Hip Hop Fiend Out


Is Hip Hop Fiend Out

2017-07-29 | ShakaLu - Editor in Chief
When did being a fiend for pharmaceutical drugs become so perversely prevalent in Hip Hop? 
It seems like every other song on popular radio makes mention of the joys of highly addictive pharmaceutical drugs like Oxycontin and Percocet and synthetic (but no less dangerous) drugs like MDMA aka Molly.  Now, let’s be clear and acknowledge there has always seemed to be an embrace of substance use in Hip Hop.  There is no denying that weed and alcohol has always been staples of Hip Hop music and commonplace within the Hip Hop community.  But it seems like things are taking a severe turn for the worse in contemporary Hip Hop.  I want to be careful and avoid presenting my concerns like there is a generational divide and only younger Hip Hop (the Seedz) heads promote this seriously dangerous behavior.  To do so would be misleading and insincere.  And I am not trying to make light of the social dangers caused by alcoholism.  But this new element of being okay with these kinds of mind altering substances is a totally different monster.
Percocet is a highly addictive narcotic prescribed to manage severe pain.  It contains oxycodone which if used in high doses can lead to respiratory distress and even death, particularly when used with alcohol. Oxycodone is an opioid, in the same family as heroin.  Basically, medically prescribed heroin.  Mollie and Ecstasy are synthetic drugs that makes a person prone to experiencing hallucinations and other psychological complications.  These are other substances that if combined with alcohol could lead to severe medical distress.
So, the question is asked again; when did the use of these sort of substances become so acceptable.  I had this conversation with a couple of colleagues and one traced the origins of this embrace to the Houston, TX based "Chopped and Screwed"movement of the early ‘90’s (a cross section between the Rootz and Treez eras).  Chopped and screwed music became synonymous with the indulgence of “Lean” of “Syrup.”  For the uninitiated, “lean/syrup” is a toxic and addictive mixture of prescription strength, codeine based cough syrup and soda.  Codeine is also a narcotic that can lead to death when mixed with other mind altering substances.  Even though there is a lot of validity to this assertion, I can’t say for sure this was the origin.
What I do know, however, if we the Hip Hop community to not take a stand and hold these artists accountable to the type of degradation they are promoting it will lead to the destruction of Hip Hop.  Think about Future’s "Mask Off".  A very catchy song but what’s disturbing is the first lyrics of this song pays some sought of blasphemous homage to pharmaceutical and synthetic drugs.  Even more disturbing is the song that Mask Off was sampled from was Charlton William’s "Prison Song". The entire premise of William’s song was to be wary and aware of evil social elements leading to the decimation of the Black community. The song is found on the “Selma” album:  A Musical Tribute to Martin Luther King, Jr.  How does a song that pays tribute to a global icon like Dr. King became bastardized with the promotion Mollie and Percocet?  Hip Hop, please be aware that there are elements in the world that want to destroy our culture.  Unfortunately, it’s some of our own shepherding this poisonous behavior as if there will be no ramifications.  To those artists, like Future, I say get your money Bro; it’s on you.  But not at the cost of Hip Hop.
#ThisIsVital
#ReflectTheCulture
#RespectTheLineage