Week 6 Assessment

Topics

Advanced JS

HTTP (Concepts Only)

You will not have to write code to make HTTP requests in JavaScript.

Definitely Not Included

Example Questions

OOP

Create a class Day

Create a subclass of Day called Holiday

Create an array of these objects to represent the week beginning with Sunday, 16 Feburary 2020.

const weekendDay = new Day('2020-02-16');
sun.isTechtonicaDay();
// => false

const weekDay = new Day('2020-02-17');
weekDay.isTechtonicaDay();
// => true

const holiday = new Holiday('2020-02-17');
holiday.isTechtonicaDay();
// => false

FP

Write a function that creates another function called createPlucker that is very similar to our extractProp method from the previous assessment but that always retrieves the same property name.

function createPlucker(propName) {
  // returns a function that takes in an array and
  //   returns an array of the values of each item's property named propName
}

const lengthPlucker = createPlucker('length');
const array = ['a', 'aa', 'aaa'];
lengthPlucker(array);
// => [1, 2, 3]

const namePlucker = createPlucker('name');
const objsWithNames = [
  { name: 'Osito', age: 14 },
  { name: 'Bella', age: 8 }
];
namePlucker(objsWithNames);
// => ['Osito', 'Bella']

Recursion

Given classes representing files and folders in a filesystem.

/**
 * Base class for any item on disk.
 * Every item (folder or file) has a name
 */
class FilesystemItem {
  constructor(name) {
    this.name = name;
  }

  isFile() {
    return false;
  }

}

class Folder extends FilesystemItem {
  constructor(name, parentFolder) {
    super(name);
    this.contents = [];
    if (parentFolder) {
      parentFolder.addItem(this);
    }
  }

  getContents() {
    return this.contents;
  }

  addItem(fileOrFolder) {
    this.contents.push(fileOrFolder);
  }

  containsFile(name) {
    // TODO: implement
  }
}

class File extends FilesystemItem {
  constructor(name, parentFolder) {
    super(name);
    parentFolder.addItem(this);
  }

  isFile() {
    return true;
  }
}

const homeFolder = new Folder('home');

const desktopFolder = new Folder('Desktop', homeFolder);
const dotProfile = new File('.profile', homeFolder);

const raccoonPic = new File('raccoon.jpg', desktopFolder);


homeFolder.containsFile('raccoon.jpg') // => true (since it's in desktop folder, which is part of home folder)
homeFolder.containsFile('nonexistent') // => false
desktopFolder.containsFile('raccoon.jpg') // => true
desktopFolder.containsFile('.profile') // => false (since it's not in desktop, only its parent)

Write a function containsFileNamed(folder, name)

containsFileNamed(home, '.profile');
// => true

containsFileNamed(desktop, '.profile');
// => false

containsFileNamed(desktop, 'raccoon.jpg');
// => true

containsFileNamed(home, 'Desktop');
// => false (because Desktop is a folder, not a file)

containsFileNamed(home, 'raccoon.jpg');
// => true

Jasmine Testing

Write Jasmine test cases for the function below. Think of all the possible cases you might need to test.

function calculateAge(birthDate) {
  const msDiff = Date.now() - birthDate;
  const ageDate = new Date(msDiff);
  return Math.abs(ageDate.getFullYear() - 1970);
}

describe('calculateAge()', function() {
  it('should do something', function() {
    expect(true).toBe(true);
  });
});

HTTP