From 32790634d2ad67b5b5eadd6cd1728db5de38f9ad Mon Sep 17 00:00:00 2001 From: Matt Garcia Date: Thu, 30 Nov 2023 20:27:17 -0700 Subject: [PATCH 1/2] Working Project, nearly all testing complete --- index.js | 4 ++ src/card.js | 66 +++++++++++++++++++ src/game.js | 24 ++++++- src/util.js | 2 +- test/card-test.js | 160 +++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 251 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index d2387f236..93cdbad4a 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,7 @@ // This is where your project starts. +const game = require("./src/game.js") +const { start } = require('./src/game'); + +start() console.log('Your project is running...'); diff --git a/src/card.js b/src/card.js index e69de29bb..7ab78ab93 100644 --- a/src/card.js +++ b/src/card.js @@ -0,0 +1,66 @@ +function createCard(id, question, answers, correctAnswer) { + return { + id : id, + question : question, + answers : answers, + correctAnswer : correctAnswer, + } +} + +function evaluateGuess(guess, correctAnswer) { +if (guess == correctAnswer) { + return "Correct!" +} + return "Incorrect!" +} + +function createDeck(deck) { + return deck +} + +function countCards(deck) { + return deck.length +} + +function createRound(deck) { + return { + deck : deck, + currentCard : deck[0], + turns : 0, + incorrectGuesses : [] + } +} + +function takeTurn(guess, round) { + const feedback = evaluateGuess(guess, round.currentCard.correctAnswer) + if (feedback == "Incorrect!") { + round.incorrectGuesses.push(round.currentCard.id) + } + round.turns++ + round.currentCard = round.deck[round.turns] + return feedback +} + +function calculatePercentCorrect(round) { + const wrong = round.incorrectGuesses.length + const total = countCards(round.deck) + const correct = total - wrong + let percentCorrect = correct / total + percentCorrect = percentCorrect * 100 + percentCorrect = Math.round(percentCorrect) + return percentCorrect +} +function endRound(round) { + return `** Round over! ** You answered ${calculatePercentCorrect(round)}% of the questions correctly!` +} + +module.exports = { + createDeck, + countCards, + createCard, + evaluateGuess, + createRound, + calculatePercentCorrect, + endRound, + takeTurn, + } \ No newline at end of file diff --git a/src/game.js b/src/game.js index acf58c6a9..7463f9e7e 100644 --- a/src/game.js +++ b/src/game.js @@ -1,6 +1,28 @@ const data = require('./data'); const prototypeQuestions = data.prototypeData; const util = require('./util'); +const { createDeck, + countCards, + createCard, + evaluateGuess, + createRound, + calculatePercentCorrect, + endRound, + takeTurn, } = require('./card'); + + +function start() { + const deck = prototypeQuestions + let round = createRound(deck) + printMessage(deck) + printQuestion(round) +} + + + + + + function printMessage(deck) { console.log(`Welcome to FlashCards! You are playing with ${countCards(deck)} cards. @@ -11,4 +33,4 @@ function printQuestion(round) { util.main(round); } -module.exports = { printMessage, printQuestion }; +module.exports = { printMessage, printQuestion, start }; diff --git a/src/util.js b/src/util.js index e974ff129..d0cff6a39 100644 --- a/src/util.js +++ b/src/util.js @@ -1,5 +1,5 @@ const inquirer = require('inquirer'); -const { takeTurn, endRound } = require('./round'); +const { takeTurn, endRound } = require('./card'); const genList = (round) => { let card = round.currentCard; diff --git a/test/card-test.js b/test/card-test.js index 3396d91d4..126a59914 100644 --- a/test/card-test.js +++ b/test/card-test.js @@ -1,14 +1,21 @@ const chai = require('chai'); const expect = chai.expect; -const { createCard } = require('../src/card'); +const { createDeck, + countCards, + createCard, + evaluateGuess, + createRound, + calculatePercentCorrect, + endRound, + takeTurn, } = require('../src/card'); describe('card', function() { - it.skip('should be a function', function() { + it('should be a function', function() { expect(createCard).to.be.a('function'); }); - it.skip('should create a card and its properties', function() { + it('should create a card and its properties', function() { const card = createCard(1, 'What allows you to define a set of related information using key-value pairs?', ['object', 'array', 'function'], 'object'); expect(card.id).to.equal(1); @@ -17,3 +24,150 @@ describe('card', function() { expect(card.correctAnswer).to.equal('object'); }); }); + +describe('card', function() { + it('should be a function', function() { + expect(evaluateGuess).to.be.a('function'); + }); + + it('should turn the flashcards to reveal answers', function() { + const card = createCard(1, 'What allows you to define a set of related information using key-value pairs?', ['object', 'array', 'function'], 'object'); + const correctResult = evaluateGuess('object', card.correctAnswer); + const incorrectResult = evaluateGuess('array', card.correctAnswer); + + + expect(correctResult).to.equal("Correct!"); + expect(incorrectResult).to.equal("Incorrect!"); + }); +}); + +describe('card', function() { + it('should be a function', function() { + expect(createDeck).to.be.a('function'); + }); + + it('should be a function', function() { + expect(countCards).to.be.a('function'); + }); + + it('should create a deck of cards', function() { + const card1 = createCard(1, 'What is Robbie\'s favorite animal', ['sea otter', 'pug', 'capybara'], 'sea otter'); + const card2 = createCard(14, 'What organ is Khalid missing?', ['spleen', 'appendix', 'gallbladder'], 'gallbladder'); + const card3 = createCard(12, 'What is Travis\'s middle name?', ['Lex', 'William', 'Fitzgerald'], 'Fitzgerald'); + const deck = createDeck([card1, card2, card3]); + + expect(deck).to.deep.equal([card1, card2, card3]); + + deckSize = countCards(deck) + + expect(deckSize).to.equal(3); + }); + + describe('createRound', () => { + it('should return an object with the specified properties', () => { + const deck = ['card1', 'card2', 'card3']; + const round = createRound(deck); + + expect(round).to.be.an('object'); + expect(round).to.have.property('deck').deep.equal(deck); + expect(round).to.have.property('currentCard').equal(deck[0]); + expect(round).to.have.property('turns').equal(0); + expect(round).to.have.property('incorrectGuesses').deep.equal([]); + }); + + it('should handle empty deck', () => { + const round = createRound([]); + + expect(round).to.be.an('object'); + expect(round).to.have.property('deck').deep.equal([]); + expect(round).to.have.property('currentCard').to.be.undefined; + expect(round).to.have.property('turns').equal(0); + expect(round).to.have.property('incorrectGuesses').deep.equal([]); + }); + + describe('takeTurn', () => { + it('should increment turns and update currentCard for a correct guess', () => { + const round = { + turns: 0, + currentCard: { id: 1, correctAnswer: 'A' }, + deck: [{ id: 1, correctAnswer: 'A' }, { id: 2, correctAnswer: 'B' }], + incorrectGuesses: [], + }; + + const feedback = takeTurn('A', round); + + expect(feedback).to.equal('Correct!'); + expect(round.turns).to.equal(1); + expect(round.currentCard).to.equal(round.deck[1]); + expect(round.incorrectGuesses).to.be.an('array').that.is.empty; + }); + + it('should increment turns and update currentCard and record incorrect guess', () => { + const round = { + turns: 0, + currentCard: { id: 1, correctAnswer: 'A' }, + deck: [{ id: 1, correctAnswer: 'A' }, { id: 2, correctAnswer: 'B' }], + incorrectGuesses: [], + }; + + const feedback = takeTurn('B', round); + + expect(feedback).to.equal('Incorrect!'); + expect(round.turns).to.equal(1); + expect(round.currentCard).to.equal(round.deck[1]); + expect(round.incorrectGuesses).to.deep.equal([1]); + }); + }); + + + +describe('calculatePercentCorrect', () => { + it('should calculate percent correct for a round with correct guesses', () => { + const round = { + incorrectGuesses: [], + deck: ['card1', 'card2', 'card3'], + }; + + const percentCorrect = calculatePercentCorrect(round); + + expect(percentCorrect).to.equal(100); + }); + + it('should calculate percent correct for a round with incorrect guesses', () => { + const round = { + incorrectGuesses: [1, 2], + deck: ['card1', 'card2', 'card3'], + }; + + const percentCorrect = calculatePercentCorrect(round); + + expect(percentCorrect).to.equal(33); + }); + + it('should handle a round with zero total cards', () => { + const round = { + incorrectGuesses: [], + deck: [], + }; + + const percentCorrect = calculatePercentCorrect(round); + + expect(percentCorrect).to.be.null; + }); + + it('should handle a round with zero correct guesses', () => { + const round = { + incorrectGuesses: [1, 2, 3], + deck: ['card1', 'card2', 'card3'], + }; + + const percentCorrect = calculatePercentCorrect(round); + + expect(percentCorrect).to.equal(0); + }); + + + +}); +}) +}) \ No newline at end of file From 597d9792ec744c50e0eac16a8f1b46b64aca76e7 Mon Sep 17 00:00:00 2001 From: Matt Garcia Date: Thu, 30 Nov 2023 20:37:57 -0700 Subject: [PATCH 2/2] recommit --- test/card-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/card-test.js b/test/card-test.js index 126a59914..aced4a33c 100644 --- a/test/card-test.js +++ b/test/card-test.js @@ -170,4 +170,4 @@ describe('calculatePercentCorrect', () => { }); }) -}) \ No newline at end of file +}) \ No newline at end of file