Update some of the exercises

This commit is contained in:
Steve Kinney
2024-09-16 13:13:38 -06:00
parent a028678660
commit 1747f705a6
15 changed files with 1617 additions and 597 deletions

View File

@@ -0,0 +1 @@
# Cool Kids Club

1420
examples/characters/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,29 @@
{
"name": "basic-math",
"version": "1.0.0",
"description": "Let's talk about adding and subtracting numbers.",
"main": "src/index.js",
"type": "module",
"scripts": {
"start": "vitest --ui",
"test": "vitest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/stevekinney/testing-javascript.git"
},
"author": "Steve Kinney <hello@stevekinney.net>",
"license": "MIT",
"bugs": {
"url": "https://github.com/stevekinney/testing-javascript/issues"
},
"homepage": "https://github.com/stevekinney/testing-javascript#readme",
"devDependencies": {
"@vitest/ui": "^2.1.1",
"vite": "^5.4.5",
"vitest": "^2.1.1"
},
"dependencies": {
"uuid": "^10.0.0"
}
}

View File

@@ -0,0 +1,14 @@
import { describe, it, expect } from 'vitest';
import { Character } from './character.js';
import { Person } from './person.js';
describe('Character', () => {
it.todo(
'should create a character with a first name, last name, and role',
() => {},
);
it.todo('should allow you to increase the level', () => {});
it.todo('should update the last modified date when leveling up', () => {});
});

View File

@@ -0,0 +1,26 @@
import { Person } from './person.js';
import { rollDice } from './roll-dice.js';
export class Character extends Person {
constructor(firstName, lastName, role) {
super(firstName, lastName);
this.role = role;
this.level = 1;
this.createdAt = new Date();
this.lastModified = this.createdAt;
this.strength = rollDice(4, 6);
this.dexterity = rollDice(4, 6);
this.intelligence = rollDice(4, 6);
this.wisdom = rollDice(4, 6);
this.charisma = rollDice(4, 6);
this.constitution = rollDice(4, 6);
}
levelUp() {
this.level++;
this.lastModified = new Date();
}
}

View File

@@ -0,0 +1,17 @@
import { v4 as id } from 'uuid';
export class Person {
constructor(firstName, lastName) {
if (!firstName || !lastName) {
throw new Error('First name and last name are required');
}
this.id = 'person-' + id();
this.firstName = firstName;
this.lastName = lastName;
}
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
}

View File

@@ -0,0 +1,27 @@
import { describe, it, expect } from 'vitest';
import { Person } from './person.js';
// Remove the `todo` from the `describe` to run the tests.
describe.todo('Person', () => {
// This test will fail. Why?
it('should create a person with a first name and last name', () => {
const person = new Person('Grace', 'Hopper');
expect(person).toEqual({
firstName: 'Grace',
lastName: 'Hopper',
});
});
it('should throw an error if first name or last name is missing', () => {
expect(() => new Person('Grace')).toThrow(
'First name and last name are required',
);
expect(() => new Person()).toThrow('First name and last name are required');
});
it('should return the full name', () => {
const person = new Person('Grace', 'Hopper');
expect(person.fullName).toBe('Grace Hopper');
});
});

View File

@@ -0,0 +1,16 @@
/**
* Rolls a specified number of dice with a specified number of sides,
* drops the lowest roll, and returns the sum of the remaining rolls.
* @param {number} diceCount - The number of dice to roll.
* @param {number} diceSides - The number of sides on each die.
*/
export const rollDice = (diceCount = 4, diceSides = 6) => {
const rolls = [];
for (let i = 0; i < diceCount; i++) {
rolls.push(Math.floor(Math.random() * diceSides) + 1);
}
rolls.sort((a, b) => a - b); // Sort rolls to drop the lowest one
return rolls.slice(1).reduce((acc, curr) => acc + curr, 0); // Sum the top 3 rolls
};

View File

@@ -0,0 +1,5 @@
export default {
test: {
environment: 'node',
},
};