Skip to content
Snippets Groups Projects
Commit de9cbee2 authored by Brady James Garvin's avatar Brady James Garvin
Browse files

Initial commit.

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 294 additions and 0 deletions
/* eslint-disable no-magic-numbers */
import { countDigits } from './whileLoops.js';
describe('the countDigits function', () => {
test('counts digits in a one-digit positive integer', () => {
expect(countDigits(5)).toBe(1);
});
test('counts digits in a two-digit positive integer', () => {
expect(countDigits(55)).toBe(2);
});
test('counts digits in a three-digit positive integer', () => {
expect(countDigits(555)).toBe(3);
});
test('counts digits in a four-digit positive integer', () => {
expect(countDigits(5555)).toBe(4);
});
test('counts digits in a five-digit positive integer', () => {
expect(countDigits(55555)).toBe(5);
});
test('counts digits in a six-digit positive integer', () => {
expect(countDigits(555555)).toBe(6);
});
test('counts digits in a seven-digit positive integer', () => {
expect(countDigits(5555555)).toBe(7);
});
test('counts digits in an eight-digit positive integer', () => {
expect(countDigits(55555555)).toBe(8);
});
});
export function triangularNumber(termCount) {
let result = 0;
// INSTRUCTIONS: Write a counter-based loop here so that the loop variable
// begins at zero, the loop variable goes up by one each iteration, and the
// loop terminates as soon as the loop variable reaches termCount. Inside the
// loop, add the loop variable to result.
return result;
}
export function triangularNumber(termCount) {
let result = 0;
for (let i = 0; i < termCount; ++i) {
result += i;
}
return result;
}
/* eslint-disable no-magic-numbers */
import { triangularNumber } from './counterBasedForLoops.js';
describe('the triangularNumber function', () => {
test('computes the zeroth triangular number', () => {
expect(triangularNumber(0)).toBe(0);
});
test('computes the first triangular number', () => {
expect(triangularNumber(1)).toBe(0);
});
test('computes the second triangular number', () => {
expect(triangularNumber(2)).toBe(1);
});
test('computes the third triangular number', () => {
expect(triangularNumber(3)).toBe(3);
});
test('computes the fourth triangular number', () => {
expect(triangularNumber(4)).toBe(6);
});
test('computes the fifth triangular number', () => {
expect(triangularNumber(5)).toBe(10);
});
test('computes the sixth triangular number', () => {
expect(triangularNumber(6)).toBe(15);
});
test('computes the seventh triangular number', () => {
expect(triangularNumber(7)).toBe(21);
});
});
export function sum(collection) {
let result = 0;
// INSTRUCTIONS: Write a loop that iterates over the given collection of
// numbers and adds each element to the result.
return result;
}
export function sum(collection) {
let result = 0;
for (const element of collection) {
result += element;
}
return result;
}
/* eslint-disable no-magic-numbers */
import { sum } from './forEachLoops.js';
describe('the sum function', () => {
test('totals elements of an empty', () => {
expect(sum([])).toBe(0);
});
test('totals elements of a nonempty list', () => {
expect(sum([4, 5, 6])).toBe(15);
});
test('totals elements of an empty set', () => {
expect(sum(new Set([]))).toBe(0);
});
test('totals elements of a nonempty set', () => {
expect(sum(new Set([6, 7, 8]))).toBe(21);
});
});
export class Book {
// INSTRUCTIONS: Write a constructor that takes two parameters, title and
// author, and stores them in two public fields, this.title and this.author.
}
export class Book {
constructor(title, author) {
this.title = title;
this.author = author;
}
}
import { Book } from './constructorsAndFields.js';
describe('the first Book class', () => {
test('has title and author fields and no others', () => {
const book = new Book('A Title', 'An Author');
expect(Object.getOwnPropertyNames(book).sort()).toEqual(['author', 'title']);
});
test('stores the title and author given at construction time', () => {
const book = new Book('A Title', 'An Author');
expect(book.title).toBe('A Title');
expect(book.author).toBe('An Author');
});
});
export class Book {
// INSTRUCTIONS: Write a constructor that takes two parameters, title and
// author, and stores them in two protected fields, this._title and
// this._author.
// INSTRUCTIONS: Write a getter so that if b is a book with title X and author
// Y, b.entry is equal to the string 'X by Y'.
}
export class Book {
constructor(title, author) {
this._title = title;
this._author = author;
}
get entry() {
return `${this._title} by ${this._author}`;
}
}
/* eslint-disable no-underscore-dangle */
import { Book } from './protectedFieldsAndGetters.js';
describe('the second Book class', () => {
test('has _title and _author fields and no others', () => {
const book = new Book('A Title', 'An Author');
expect(Object.getOwnPropertyNames(book).sort()).toEqual(['_author', '_title']);
});
test('stores the title and author given at construction time', () => {
const book = new Book('A Title', 'An Author');
expect(book.entry).toBe('A Title by An Author');
});
test('computes its entry based on the most recent title and author data', () => {
const book = new Book('A Title', 'An Author');
book._title = 'A New Title';
expect(book.entry).toBe('A New Title by An Author');
});
});
const FAHRENHEIT_FREEZING = 32;
const FAHRENHEIT_PER_CELSIUS = 1.8;
export class Temperature {
constructor(celsius) {
this.celsius = celsius;
}
get fahrenheit() {
return FAHRENHEIT_FREEZING + FAHRENHEIT_PER_CELSIUS * this.celsius;
}
// INSTRUCTIONS: Write a setter so that if t is a temperature, a statement
// like t.fahrenheit = 32; will set t.celsius to the equivalent temperature in
// Celsius.
}
const FAHRENHEIT_FREEZING = 32;
const FAHRENHEIT_PER_CELSIUS = 1.8;
export class Temperature {
constructor(celsius) {
this.celsius = celsius;
}
get fahrenheit() {
return FAHRENHEIT_FREEZING + FAHRENHEIT_PER_CELSIUS * this.celsius;
}
set fahrenheit(fahrenheit) {
this.celsius = (fahrenheit - FAHRENHEIT_FREEZING) / FAHRENHEIT_PER_CELSIUS;
}
}
/* eslint-disable no-magic-numbers */
import { Temperature } from './setters.js';
describe('the Temperature class', () => {
test('has a celsius field and no others', () => {
const temperature = new Temperature(20);
expect(Object.getOwnPropertyNames(temperature).sort()).toEqual(['celsius']);
});
test('stores the Celsius temperature given at construction time', () => {
const temperature = new Temperature(20);
expect(temperature.celsius).toBe(20);
});
test('provides a getter for the equivalent Fahrenheit temperature', () => {
const temperature = new Temperature(20);
expect(temperature.fahrenheit).toBe(68);
});
test('supports modifications by setting an object\'s celsius field', () => {
const temperature = new Temperature(20);
temperature.celsius = 15;
expect(temperature.celsius).toBe(15);
expect(temperature.fahrenheit).toBe(59);
});
test('supports modifications via a fahrenheit setter', () => {
const temperature = new Temperature(20);
temperature.fahrenheit = 59;
expect(Object.getOwnPropertyNames(temperature).sort()).toEqual(['celsius']);
expect(temperature.celsius).toBe(15);
expect(temperature.fahrenheit).toBe(59);
});
});
export class PlanarVector {
constructor(x, y) {
this.x = x;
this.y = y;
}
// INSTRUCTIONS: Write a dotProduct method so that if u and v are vectors,
// u.dotProduct(v) is equivalent to (u.x * v.x + u.y + v.y).
}
export class PlanarVector {
constructor(x, y) {
this.x = x;
this.y = y;
}
dotProduct(other) {
return this.x * other.x + this.y * other.y;
}
}
/* eslint-disable no-magic-numbers */
import { PlanarVector } from './methods.js';
describe('the first PlanarVector class', () => {
test('correctly computes dot products between parallel vectors', () => {
const u = new PlanarVector(1, 4);
const v = new PlanarVector(-2, -8);
expect(u.dotProduct(v)).toBe(-34);
});
test('correctly computes dot products between orthogonal vectors', () => {
const u = new PlanarVector(1, 4);
const v = new PlanarVector(8, -2);
expect(u.dotProduct(v)).toBe(0);
});
test('correctly computes dot products between vectors at an angle', () => {
const u = new PlanarVector(1, 4);
const v = new PlanarVector(5, 9);
expect(u.dotProduct(v)).toBe(41);
});
});
export class PlanarVector {
constructor() {
this.coordinates = [0, 0];
}
get x() {
// INSTRUCTIONS: Make this getter return the first number in
// this.coordinates.
}
set x(x) {
// INSTRUCTIONS: Make this getter update the first number in
// this.coordinates.
}
get y() {
// INSTRUCTIONS: Make this getter return the second number in
// this.coordinates.
}
set y(y) {
// INSTRUCTIONS: Make this getter update the second number in
// this.coordinates.
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment