Elvenware

JavaScriptObjects

Welcome to JavaScriptObjects

Java Script Objects

In this assignment we will learn about JavaScript objects.

These slide decks have useful information in them:

Also see the section of Elvenware on Object Literals, and consider reading the entire opening sections of the Elvenware JavaScript Objects document:

The Elvenware JavaScript Objects document is a primary reference for this assignment. Note also, that in this exercise, we are mostly working with object literals, not function or constructor objects.

Part One

Create or use a folder called one of the following:

Inside it create a single file called work.js. In that file build a JavaScript object literal called person with two properties:

Set them to your first and last names. Write the two properties to the console with console.log().

NOTE: You do not have to run this code from an HTML file or as an Express program in a browser. But if you want to do things that way, I will not take off for it. But really, it is simpler for me if this program just stays in a single nodejs file called work.js that runs from the command line. Some students in advanced courses might want to use other tools such as EsLint or Prettier.

Now run it:

cd Week02-JavaScriptObjects
node work.js

The output should look something like this:

George
Washington

Part Two

Add a fullName method to the person object. Make it part of the Object literal. The method should concatenate the first and last names, and return the result. Call the method and print out the return value with console.log.

node work.js
George
Washington
George Washington

Part Three

In the same file, using JavaScript Object Literal syntax, create a new object called calculator.

Inside the object literal, create two properties called:

Initialize these properties to -1, much as we initialized the name property of person to 'Suzie' or some similar name in the first of the two examples in Part Zero. Of course, this property is of type number, not of type string.

HINT: See this slide for help understanding what I want.

Inside your program, but outside the object literal declaration, set the properties to the number of letters in your first and last name using dot notation:

calculator.operand01 = person.firstName.length;
etc...

Use console.log to display the values of operand01 and operand02.

Simple Math

Add three methods with zero parameters that operate on calculator.operand01 and calculator.operand02:

Put add and subtract inside the object literal and implement multiply outside the object literal declaration using dot notation:

var calculator = {
    // CODE OMITTED HERE
    // PUT ADD AND SUBTRACT IN HERE
};

calculator.multiply = etc.... // IMPLEMENT MULTIPLY HERE

Print the output. For instance, since my name is Charlie Calvert

14
0
49

Gussie it up

I added this method:

function divider(title) {
    console.log("====================================");
    console.log(title);
    console.log("====================================");
}

And made some calls like this:

divider('Calculator');
console.log('operand01 =', calculator.operand01);

And produced output like this:

$ node work.js
====================================
Person
====================================
George
Washington
George Washington
====================================
Calculator
====================================
operand01 = 6
operand02 = 10
Add:  16
Subtract: -4
Multiply: 60

Unless you name is George Washington, your output will probably differ, but this should give you some general idea of what to produce.

Grunt Check

NOTE: After Spring 2017, we are not using Grunt as often, but instead we are switching to eslint, prettier and webpack. As a result, you should skip this section.

As always, use Grunt to make sure your code is properly formatted. If you have not yet completed the GruntCheck assignment, do so before you turn this assignment in. The point is that you will need to understand the Grunt Check assignment before you can complete this assignment.

NOTE: Previously, in Grunt Check, I said "in this and all future projects" be sure you code passes grunt check. I will take off points if your code is clearly improperly formatted. Grunt check was created to help you do the right thing. It is easier, I think, if we run it than if we don't. To make it work, of course, you need .jscsrc and Gruntfile.js plus a package.json file. But these are boiler plate at this time.

Turn it in

Save your work to your repository. Submit the url of your project and/or the directory in which you did your work.

Take a screenshot of the output of your program and attach it to the assignment when you turn it in.

In summary:

If you unclear what all this means, look at the JavaScript Basics slide deck again.

Hint for Linux Users

You can put a hash bang at the top of a file like work.js:

#!/usr/bin/env node

var person = {
    etc...

Then mark it as executable:

chmod +x work.js

Now you can start it like this:

./work.js