Git Product home page Git Product logo

iterators_lab's Introduction

Iterators Lab

To get setup you want to practice using assertions to test your iterators. We are going to write our code and tests in the test/ folder.

Let's try runnig the test/example.js file

node test/example.js
"Example is working"

Using Assertions To Test

If you look at the code in test/example.js then you will see the following:

var assert = require("assert");
var colors = require("colors");


// Write your code here
var person = {};
person.firstName = "Jane";
person.lastName = "Doe";
person.fullName = function () {
  return this.lastName + ", " + this.firstName;
};

// Write your assertions aferward
// check person.firstName is Jane
assert.equal(person.firstName, "Jane", "firstName should be 'Jane'".red);
// check person.lastName is Doe
assert.equal(person.lastName, "Doe", "lastName should be 'Doe'".red);
// check person.fullName returns "Doe, Jane"
assert.equal(person.fullName(), "Doe, Jane", "fullName should be 'Doe, Jane'".red);

console.log("Example is working!".cyan);

Try modifying the code in one of the asserts from test/example.js. For example let's change the last assertion to say "Jane Doe" instead of "Doe, Jane".

...
assert.equal(person.fullName(), "Jane Doe", "fullName should be 'Doe, Jane'".red);
...

If you run the code using node/example.js then you'll get the following error

assert.js:92
  throw new assert.AssertionError({
        ^
AssertionError: fullName should be 'Doe, Jane'
    at Object.<anonymous> (/Users/delmerreed/Documents/GA_Work/wdi_sf_15/iterators/test/example.js:19:8)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

Note how it says the following:

AssertionError: fullName should be 'Doe, Jane'

We can then use our assert statements to test if our example is behaving correctly, because if it isn't we get an error. If we have no errors then we make to final console.log("Example is working!".cyan);.

More On Assert

Assert is method that is built into node to compare values. It can be used one of two ways.

assert.equal(1,2)

The above way just throws an error because 1 == 2 doesn't return true. We can also say the following.

assert.equal(1,2, "custom message")

Now when the assert.equal fails to be true it will print our "custom message" string to remind us what we were trying to test.

Directions

To get setup fork and clone this repo then run npm install.

In this homework you'll be implementing some useful function that are commonly used in making web applications: first, last, indexOf, last, and of course, each and map.

Your job will be to use the assert.equal method to write assertions that test your function is working correctly. Each file can be run using

node test/some_file_name.js

Each file includes the following two helpful lines of code

var assert = require("assert");
var colors = require("colors");

This is helpful if you want to print out cool and colorful error messages to the console.

iterators_lab's People

Contributors

dchacke-ga avatar karenchu avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.