Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,71 @@ export function doubleEach ( arr ) {
return result;
}

// start simple

//export function triple (x) {
// return x * 3;
//}
//
//export function tripleEach (arr) {
// let result = arr.map (triple);
// return result;
//}

// lesson 1 - activity make Animal work. Constructors and Prototypes
// what I did:
//
//export function Animal ( animal,speak ) {
// this.speak = function(){
// return "generic sound";
// }
//
//}

export function Animal (){
}

Animal.prototype.speak = function(){
return 'generic sound';
}

// create new constructors with prototypes that inherit from Animal Reptile and Primate. Overwrite the speak method for each.

export function Reptile(){
// let SOUND = 'hiss';
}

Reptile.prototype = new Animal(); // inherit Animal properties if you get created
Reptile.prototype.constructor = Reptile; // Make a valid reptile which is a new animal
Reptile.SOUND = 'hissss';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. I couldn't think of a good reptile sound.

// you can create a function same as parent

Reptile.prototype.speak = function(){
return Reptile.SOUND;
}

export function Primate(){
}

Primate.prototype = new Animal();
Primate.prototype.constructor = Primate;
Primate.SOUND = "bongos";

Primate.prototype.speak = function (){
return Primate.SOUND;
}


// Create a human

export function Human(){

}

Human.prototype = new Primate();
Human.prototype.constructor = Human;
Human.message = 'hello';

Human.prototype.speak = function(){
return Human.message;
}
82 changes: 81 additions & 1 deletion src/spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'tape';
import { double, doubleXTimes, doubleEach } from './index';
import { double, doubleXTimes, doubleEach, tripleEach, Animal, Reptile, Primate, Human } from './index';

test( 'double fn', function ( test ) {
const actual = double( 5 );
Expand Down Expand Up @@ -28,3 +28,83 @@ test( 'doubleEach', function ( test ) {
test.end();
});

// just checking if I can 'copy paste'!
//
//test ( 'tripleEach', function ( test ) {
// const actual = tripleEach([1, 3 ,6]);
// const expected = [3, 9, 18];
//
// test.deepEqual( actual, expected, 'should triple each in the array');
//
// test.end();
//});

// lesson 1 activity - try and make test pass.

test ( 'Animal', function ( test ) {
const animal = new Animal (); // constructor

test.ok( animal instanceof Animal, 'should create an instance of Animal');

// console.log(animal);

test.end();
});

test ('Animal', function (test) {
const animal = new Animal ();
const expected = 'generic sound';
const actual = animal.speak();

test.equal( actual, expected, 'should make a generic sound when it speaks');

test.end();
});



// inheritance
//
//

test ('Inheritance', function (test) {
let actual, expected;


const reptile = new Reptile();

test.ok( reptile instanceof Reptile, 'should be an instance of Reptile' );
test.ok( reptile instanceof Animal, 'should be an instance of Animal' );

expected = Reptile.SOUND;
actual = reptile.speak();
test.equal( actual, expected, 'should make a reptile sound when it speaks' );

const primate = new Primate();

test.ok( primate instanceof Primate, 'should be an instance of Primate' );
test.ok( primate instanceof Animal, 'should be an instance of Animal' );

expected = Primate.SOUND;
actual = primate.speak();
test.equal( actual, expected, 'should make a primate sound when it speaks' );
test.end();
});

test ('Inheritance Human', function (test){

let actual, expected;

const human = new Human();

test.ok( human instanceof Human, 'should be an instance of Human' );
test.ok( human instanceof Primate, 'should be an instance of Primate' );
test.ok( human instanceof Animal, 'should be an instance of Animal' );

const message = 'hello';
expected = message;
actual = human.speak( message );
test.equal( actual, expected, 'should speak what it is asked to speak' );
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem quite right. Why are you passing in the message as an argument, but not using it in your function?


test.end()
});