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

Initial commit.

parents
Branches
No related tags found
No related merge requests found
Showing
with 241 additions and 0 deletions
export class PlanarVector {
constructor() {
this.coordinates = [0, 0];
}
get x() {
return this.coordinates[0];
}
set x(x) {
this.coordinates[0] = x;
}
get y() {
return this.coordinates[1];
}
set y(y) {
this.coordinates[1] = y;
}
}
/* eslint-disable no-magic-numbers */
import { PlanarVector } from './arrays.js';
describe('the second PlanarVector class', () => {
test('correctly gets the x coordinate', () => {
const v = new PlanarVector();
v.coordinates = [4, 5];
expect(v.x).toBe(4);
});
test('correctly sets the x coordinate', () => {
const v = new PlanarVector();
v.x = 4;
expect(v.coordinates).toEqual([4, 0]);
});
test('correctly gets the y coordinate', () => {
const v = new PlanarVector();
v.coordinates = [4, 5];
expect(v.y).toBe(5);
});
test('correctly sets the y coordinate', () => {
const v = new PlanarVector();
v.y = 5;
expect(v.coordinates).toEqual([0, 5]);
});
});
export class Swappable {
constructor(first, second) {
this.first = first;
this.second = second;
}
swap() {
// INSTRUCTIONS: Write a single line of code to swap this.first and
// this.second.
}
}
export class Swappable {
constructor(first, second) {
this.first = first;
this.second = second;
}
swap() {
[this.first, this.second] = [this.second, this.first];
}
}
/* eslint-disable no-magic-numbers */
import { Swappable } from './arrayDestructuring.js';
describe('the Swappable class', () => {
test('stores the two values given at construction time', () => {
const swappable = new Swappable(4, 5);
expect(swappable.first).toBe(4);
expect(swappable.second).toBe(5);
});
test('can swap the two values', () => {
const swappable = new Swappable(4, 5);
swappable.swap();
expect(swappable.first).toBe(5);
expect(swappable.second).toBe(4);
});
});
export function countDistinctElements(list) {
// INSTRUCTIONS: Write a single return statement that converts the list to a
// set (to remove duplicates) and then returns the size of the set.
}
export function countDistinctElements(list) {
return new Set(list).size;
}
/* eslint-disable no-magic-numbers */
import { countDistinctElements } from './sets.js';
describe('the countDistinctElements function', () => {
test('counts distinct elements in an empty list', () => {
expect(countDistinctElements([])).toBe(0);
});
test('counts distinct elements in list without repeats', () => {
expect(countDistinctElements([6, 4, 3, 5])).toBe(4);
});
test('counts distinct elements in list with repeats', () => {
expect(countDistinctElements([6, 4, 6, 3, 4, 5, 4, 4])).toBe(4);
});
});
export function findMostCommonCharacter(string) {
const histogram = new Map();
for (const character of string) {
histogram.set(character, 0);
}
// INSTRUCTIONS: Write code to loop over the characters in the string and, for
// each character, increment the corresponding value in histogram.
// INSTRUCTIONS: Write code to find and return the most common character in
// string based on the counts in histogram, or undefined if the string is
// empty. (If there is are ties, the code may return any one of the most
// common characters.)
}
export function findMostCommonCharacter(string) {
const histogram = new Map();
for (const character of string) {
histogram.set(character, 0);
}
for (const character of string) {
histogram.set(character, histogram.get(character) + 1);
}
let mostCommon = undefined;
let highestCount = 0;
for (const [character, count] of histogram) {
if (count > highestCount) {
mostCommon = character;
highestCount = count;
}
}
return mostCommon;
}
import { findMostCommonCharacter } from './maps.js';
describe('the findMostCommonCharacter function', () => {
test('returns undefined if the string is empty', () => {
expect(findMostCommonCharacter('')).toBe(undefined);
});
test('returns the lone character if the string has length one', () => {
expect(findMostCommonCharacter('a')).toBe('a');
});
test('returns the repeated character if the string has only one duplicated character', () => {
expect(findMostCommonCharacter('cinematography')).toBe('a');
});
test('returns the most repeated character if the string has multiple duplicated characters', () => {
expect(findMostCommonCharacter('electroencephalograph')).toBe('e');
});
});
export function summarize(record) {
// INSTRUCTIONS: Write a single assignment statement to extract record.type
// and record.name to the variables type and name.
return `Record of type ${type}: ${name}`;
}
export function summarize(record) {
const {
type,
name,
} = record;
return `Record of type ${type}: ${name}`;
}
import { summarize } from './objectDestructuring.js';
describe('the summarize function', () => {
test('extracts an undefined type and undefined name from an empty object', () => {
expect(summarize({})).toBe('Record of type undefined: undefined');
});
test('extracts a type from an object even if the object has no name', () => {
expect(summarize({
type: 'Foo',
})).toBe('Record of type Foo: undefined');
});
test('extracts a name from an object even if the object has no type', () => {
expect(summarize({
name: 'Bar',
})).toBe('Record of type undefined: Bar');
});
test('extracts a type and a name from an object with both', () => {
expect(summarize({
type: 'Foo',
name: 'Bar',
})).toBe('Record of type Foo: Bar');
});
test('extracts a type and a name from an object with those and other fields', () => {
expect(summarize({
type: 'Foo',
name: 'Bar',
value: Infinity,
})).toBe('Record of type Foo: Bar');
});
});
// INSTRUCTIONS: Change the function signature so that the progression starts at
// zero if no starting value is given, and its terms go up by one if no
// increment is given.
export function formatArithmeticProgression(start, increment) {
return `${start}, ${start + increment}, ${start + 2 * increment}, …`;
}
export function formatArithmeticProgression(start = 0, increment = 1) {
return `${start}, ${start + increment}, ${start + 2 * increment}, …`;
}
/* eslint-disable no-magic-numbers */
import { formatArithmeticProgression } from './defaultParameters.js';
describe('the formatArithmeticProgression function', () => {
test('formats a progression with an explicit start and increment', () => {
expect(formatArithmeticProgression(9, 2)).toBe('9, 11, 13, …');
});
test('formats a progression with an implicit increment', () => {
expect(formatArithmeticProgression(9)).toBe('9, 10, 11, …');
});
test('formats a progression with an implicit start and increment', () => {
expect(formatArithmeticProgression()).toBe('0, 1, 2, …');
});
});
// INSTRUCTIONS: Change the function signature so that the function counts the
// number of arguments it is given.
export function countArguments() {
return values.length;
}
export function countArguments(...values) {
return values.length;
}
import { countArguments } from './restParameters.js';
describe('the countArguments function', () => {
test('counts to zero if called with no arguments', () => {
expect(countArguments()).toBe(0);
});
test('counts to one if called with one argument', () => {
expect(countArguments('foo')).toBe(1);
});
test('counts to two if called with two arguments', () => {
expect(countArguments('foo', 'bar')).toBe(2);
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment