From c3013a0fb83b673768d970491584916d1cfde3a4 Mon Sep 17 00:00:00 2001 From: Ricky Tran Date: Tue, 28 Nov 2023 13:33:38 -0700 Subject: [PATCH 1/3] Initial commit --- src/card.js | 13 +++++++++++++ src/deck.js | 8 ++++++++ src/turns.js | 15 +++++++++++++++ test/card-test.js | 5 ++--- test/deck-test.js | 30 ++++++++++++++++++++++++++++++ test/turns-test.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 src/deck.js create mode 100644 src/turns.js create mode 100644 test/deck-test.js create mode 100644 test/turns-test.js diff --git a/src/card.js b/src/card.js index e69de29bb..a19803c1c 100644 --- a/src/card.js +++ b/src/card.js @@ -0,0 +1,13 @@ +const createCard = (id, question, answers, correctAnswer) => { + const card = { + id, + question, + answers, + correctAnswer + } + return card +} + +module.exports = { + createCard +} \ No newline at end of file diff --git a/src/deck.js b/src/deck.js new file mode 100644 index 000000000..943ef8861 --- /dev/null +++ b/src/deck.js @@ -0,0 +1,8 @@ +const createDeck = (cards) => cards + +const countCards = (deck) => deck.length + +module.exports = { + createDeck, + countCards +} \ No newline at end of file diff --git a/src/turns.js b/src/turns.js new file mode 100644 index 000000000..052b7e5e6 --- /dev/null +++ b/src/turns.js @@ -0,0 +1,15 @@ +const evaluateGuess = (guess, correctAnswer) => { + if (guess) { + if (guess.toLowerCase() === correctAnswer) { + return `Correct!` + } else { + return `Incorrect!` + } + } else { + return `Please choose an answer` + } +} + +module.exports = { + evaluateGuess +} \ No newline at end of file diff --git a/test/card-test.js b/test/card-test.js index 3396d91d4..a412727ec 100644 --- a/test/card-test.js +++ b/test/card-test.js @@ -1,14 +1,13 @@ const chai = require('chai'); const expect = chai.expect; - const { createCard } = 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); diff --git a/test/deck-test.js b/test/deck-test.js new file mode 100644 index 000000000..b3ad6dfbf --- /dev/null +++ b/test/deck-test.js @@ -0,0 +1,30 @@ +const chai = require('chai') +const expect = chai.expect +const { countCards, createDeck } = require('../src/deck.js') +const { createCard } = require('../src/card.js') + +describe("deck functions", () => { + it("should create a deck with the created cards.", () => { + 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[0]).to.equal(card1) + expect(deck[1]).to.equal(card2) + expect(deck[2]).to.equal(card3) + + }) + + it("should count how many cards are in the deck.", () => { + 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]); + const cardCount = countCards(deck) + + expect(cardCount).to.equal(3) + + + }) +}) \ No newline at end of file diff --git a/test/turns-test.js b/test/turns-test.js new file mode 100644 index 000000000..b46c8d634 --- /dev/null +++ b/test/turns-test.js @@ -0,0 +1,42 @@ +const chai = require ('chai'); +const expect = chai.expect; +const { evaluateGuess } = require('../src/turns.js') + +describe("evaluateGuess()", () => { + it("Should evaluate if a guess to a flashcard is correct.", () => { + const result = evaluateGuess("object", "array") + + expect(result).to.equal("Incorrect!") + }) + + it("Should evaluate if a guess to a flashcard is incorrect.", () => { + const result = evaluateGuess("array", "array") + + expect(result).to.equal("Correct!") + }) + + it("Should not matter if a guess uppercase or lowercase.", () => { + const result1 = evaluateGuess("Array", "array") + const result2 = evaluateGuess("aRRay", "array") + + expect(result1).to.equal("Correct!") + expect(result2).to.equal("Correct!") + }) + + it("Should check if guess is empty.", () => { + let guess = "" + let correctAnswer = "object" + const result = evaluateGuess(guess, correctAnswer) + + expect(result).to.equal("Please choose an answer") + }) + + // it("Should convert guess to a string type.", () => { + // let guess = 2 + // let correctAnswer = "object" + // const result = evaluateGuess(guess, correctAnswer) + + // expect(typeof(guess)).to.equal("number") + // }) + +}) \ No newline at end of file From d4702f919f3cc6e0b5de432e2a1f4126abe33338 Mon Sep 17 00:00:00 2001 From: Ricky Tran Date: Thu, 30 Nov 2023 14:51:49 -0700 Subject: [PATCH 2/3] feat: mvp --- index.js | 4 +- package.json | 3 +- src/game.js | 15 ++++- src/round.js | 50 +++++++++++++++ src/turns.js | 3 +- test/deck-test.js | 4 +- test/game-test.js | 14 +++++ test/round-test.js | 153 +++++++++++++++++++++++++++++++++++++++++++++ test/turns-test.js | 13 +--- 9 files changed, 241 insertions(+), 18 deletions(-) create mode 100644 src/round.js create mode 100644 test/game-test.js create mode 100644 test/round-test.js diff --git a/index.js b/index.js index d2387f236..f3476f4c3 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,5 @@ // This is where your project starts. - +const { start } = require('./src/game'); console.log('Your project is running...'); + +start() \ No newline at end of file diff --git a/package.json b/package.json index cff01bd8b..c29e43253 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "OOP Flashcards Application", "main": "index.js", "scripts": { - "test": "mocha" + "test": "mocha", + "watch": "mocha --watch 'test/*.js'" }, "keywords": [ "javascript", diff --git a/src/game.js b/src/game.js index acf58c6a9..3f16bae05 100644 --- a/src/game.js +++ b/src/game.js @@ -1,6 +1,10 @@ const data = require('./data'); const prototypeQuestions = data.prototypeData; const util = require('./util'); +const { createDeck, countCards } = require('../src/deck'); +const { createRound } = require('./round'); + + function printMessage(deck) { console.log(`Welcome to FlashCards! You are playing with ${countCards(deck)} cards. @@ -11,4 +15,13 @@ function printQuestion(round) { util.main(round); } -module.exports = { printMessage, printQuestion }; +function start() { + const deck = createDeck(prototypeQuestions) + const round = createRound(deck) + + printMessage(deck) + printQuestion(round) + +} + +module.exports = { printMessage, printQuestion, start }; diff --git a/src/round.js b/src/round.js new file mode 100644 index 000000000..22dd15b56 --- /dev/null +++ b/src/round.js @@ -0,0 +1,50 @@ +const { evaluateGuess } = require('../src/turns') +const { countCards } = require('../src/deck') + +const createRound = (deck) => { + const round = { + deck, + currentCard: deck[0], + turns: 0, + incorrectGuesses: [], + } + return round +} + +const takeTurn = (guess, round) => { + const numCards = countCards(round.deck); + const resultofGuess = evaluateGuess(guess, round.currentCard.correctAnswer) + + if (resultofGuess === "Incorrect!") { + round.incorrectGuesses.push(round.currentCard.id) + } + + round.turns += 1 + round.currentCard = round.deck[round.turns] + + // if (round.turns === numCards) { + // const percentageCorrect = calculatePercentageCorrect(round) + // console.log(resultofGuess); + // console.log(endRound(percentageCorrect)) + + // } + return resultofGuess +} + +const calculatePercentageCorrect = (round) => { + const percentageCorrect = (round.deck.length - round.incorrectGuesses.length) / (round.deck.length) * 100 + return Number(percentageCorrect.toFixed(2)) +} + +function endRound(round) { + + console.log(`** Round over! ** You answered ${calculatePercentageCorrect(round)}% of the questions correctly!`); + return `** Round over! ** You answered ${calculatePercentageCorrect(round)}% of the questions correctly!`; +} + +module.exports = { + createRound, + takeTurn, + calculatePercentageCorrect, + endRound +} \ No newline at end of file diff --git a/src/turns.js b/src/turns.js index 052b7e5e6..2f3c87ad0 100644 --- a/src/turns.js +++ b/src/turns.js @@ -1,6 +1,7 @@ const evaluateGuess = (guess, correctAnswer) => { if (guess) { - if (guess.toLowerCase() === correctAnswer) { + guess = guess.toLowerCase() + if (guess == correctAnswer) { return `Correct!` } else { return `Incorrect!` diff --git a/test/deck-test.js b/test/deck-test.js index b3ad6dfbf..4ee3061b5 100644 --- a/test/deck-test.js +++ b/test/deck-test.js @@ -22,9 +22,7 @@ describe("deck functions", () => { const card3 = createCard(12, 'What is Travis\'s middle name?', ['Lex', 'William', 'Fitzgerald'], 'Fitzgerald'); const deck = createDeck([card1, card2, card3]); const cardCount = countCards(deck) - + expect(cardCount).to.equal(3) - - }) }) \ No newline at end of file diff --git a/test/game-test.js b/test/game-test.js new file mode 100644 index 000000000..af2f48ae6 --- /dev/null +++ b/test/game-test.js @@ -0,0 +1,14 @@ +const chai = require('chai') +const data = require('../src/data'); +const prototypeQuestions = data.prototypeData; +const expect = chai.expect +const { createRound, takeTurn, calculatePercentageCorrect, endRound } = require('../src/round') +const { createDeck } = require('../src/deck'); +const { start } = require('../src/game'); + +describe("game function", () => { + it("should create cards") + + const startGame = start() + console.log(startGame); +}) \ No newline at end of file diff --git a/test/round-test.js b/test/round-test.js new file mode 100644 index 000000000..2820d0869 --- /dev/null +++ b/test/round-test.js @@ -0,0 +1,153 @@ +const chai = require('chai') +const expect = chai.expect +const { createRound, takeTurn, calculatePercentageCorrect, endRound } = require('../src/round') +const { createCard } = require('../src/card.js') + +describe("The round object will organize guesses and records if they are correct or incorrect", () => { + it('should have a deck property that holds onto the deck object', () => { + //Set Up + 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 = [card1, card2, card3]; + const round1 = createRound(deck) + const deckProperty = round1.deck + + expect(deckProperty).to.equal(deck) + }) + + it('currentCard property should start as the first card in the deck', () => { + //Set Up + 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 = [card1, card2, card3]; + const round1 = createRound(deck) + const currentCardProperty = round1.currentCard + + expect(currentCardProperty).to.equal(deck[0]) + }) + + it('turns property should start at 0', () => { + //Set Up + 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 = [card1, card2, card3]; + const round1 = createRound(deck) + const turnsProperty = round1.turns + + expect(turnsProperty).to.equal(0) + }) + + it('incorrectGuesses property should start as an empty array', () => { + //Set Up + 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 = [card1, card2, card3]; + const round1 = createRound(deck) + const incorrectGuessesProperty = round1.incorrectGuesses + + expect(incorrectGuessesProperty).to.deep.equal([]) + }) + +}) + +describe("takeTurn function", () => { + it("when a guess is made, the turns count is updated", () => { + 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 = [card1, card2, card3]; + const round = createRound(deck) + const guess = 'guess' + + takeTurn(guess, round) + takeTurn(guess, round) + + expect(round.turns).to.equal(2) + + + }) + + it("when a guess is made, the next card becomes the current card", () => { + 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 = [card1, card2, card3]; + const round = createRound(deck) + const guess = 'guess' + + takeTurn(guess, round) + takeTurn(guess, round) + + expect(round.currentCard).to.equal(card3) + }) + + it("when a guess is made, incorrect guesses will be stored in incorrectGuesses", () => { + 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 = [card1, card2, card3]; + const round = createRound(deck) + const wrongGuess = 'guess' + const correctGuess = "fitzgerald" + + takeTurn(wrongGuess, round) + takeTurn(wrongGuess, round) + takeTurn(correctGuess, round) + + expect(round.incorrectGuesses).to.deep.equal([1, 14]) + }) + +}) +describe("calculatePercentageCorrect function", () => { + it("should calculate and return the percentage of correct guesses", () => { + 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 = [card1, card2, card3]; + const round = createRound(deck) + const wrongGuess = 'guess' + const correctGuess = "fitzgerald" + + takeTurn(wrongGuess, round) + takeTurn(wrongGuess, round) + takeTurn(correctGuess, round) + + const result = calculatePercentageCorrect(round) + + expect(result).to.equal(.33) + }) +}) + +describe("endRound function", () => { + it('should return a statement with % of correct answers', () => { + 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 = [card1, card2, card3]; + const round = createRound(deck) + const wrongGuess = 'guess' + const correctGuess = "fitzgerald" + + takeTurn(wrongGuess, round) + takeTurn(wrongGuess, round) + takeTurn(correctGuess, round) + + const percentCorrect = calculatePercentageCorrect(round) + const result = endRound(percentCorrect) + + expect(result).to.equal("** Round over! ** You answered 33% of the questions correctly!") + }) +}) \ No newline at end of file diff --git a/test/turns-test.js b/test/turns-test.js index b46c8d634..a819d0453 100644 --- a/test/turns-test.js +++ b/test/turns-test.js @@ -2,7 +2,7 @@ const chai = require ('chai'); const expect = chai.expect; const { evaluateGuess } = require('../src/turns.js') -describe("evaluateGuess()", () => { +describe("evaluateGuess function", () => { it("Should evaluate if a guess to a flashcard is correct.", () => { const result = evaluateGuess("object", "array") @@ -17,7 +17,7 @@ describe("evaluateGuess()", () => { it("Should not matter if a guess uppercase or lowercase.", () => { const result1 = evaluateGuess("Array", "array") - const result2 = evaluateGuess("aRRay", "array") + const result2 = evaluateGuess("fitzGerald", "fitzgerald") expect(result1).to.equal("Correct!") expect(result2).to.equal("Correct!") @@ -30,13 +30,4 @@ describe("evaluateGuess()", () => { expect(result).to.equal("Please choose an answer") }) - - // it("Should convert guess to a string type.", () => { - // let guess = 2 - // let correctAnswer = "object" - // const result = evaluateGuess(guess, correctAnswer) - - // expect(typeof(guess)).to.equal("number") - // }) - }) \ No newline at end of file From bdcd4458dd1255b4017135285eedc98d6b299b55 Mon Sep 17 00:00:00 2001 From: Ricky Tran Date: Thu, 30 Nov 2023 15:18:46 -0700 Subject: [PATCH 3/3] remove console logs. --- src/game.js | 2 +- src/round.js | 14 ++++++-------- test/round-test.js | 7 +++---- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/game.js b/src/game.js index 3f16bae05..00369595f 100644 --- a/src/game.js +++ b/src/game.js @@ -18,7 +18,7 @@ function printQuestion(round) { function start() { const deck = createDeck(prototypeQuestions) const round = createRound(deck) - + printMessage(deck) printQuestion(round) diff --git a/src/round.js b/src/round.js index 22dd15b56..56cbb294a 100644 --- a/src/round.js +++ b/src/round.js @@ -22,22 +22,20 @@ const takeTurn = (guess, round) => { round.turns += 1 round.currentCard = round.deck[round.turns] - // if (round.turns === numCards) { - // const percentageCorrect = calculatePercentageCorrect(round) - // console.log(resultofGuess); - // console.log(endRound(percentageCorrect)) - - // } return resultofGuess } const calculatePercentageCorrect = (round) => { - const percentageCorrect = (round.deck.length - round.incorrectGuesses.length) / (round.deck.length) * 100 + const deckLength = round.deck.length + const incorrectGuessesLength = round.incorrectGuesses.length + const percentageCorrect = ((deckLength - incorrectGuessesLength) / deckLength) * 100 + return Number(percentageCorrect.toFixed(2)) } function endRound(round) { - + const percentCorrect = calculatePercentageCorrect(round) + console.log(`** Round over! ** You answered ${calculatePercentageCorrect(round)}% of the questions correctly!`); return `** Round over! ** You answered ${calculatePercentageCorrect(round)}% of the questions correctly!`; } diff --git a/test/round-test.js b/test/round-test.js index 2820d0869..e662ff5a1 100644 --- a/test/round-test.js +++ b/test/round-test.js @@ -126,7 +126,7 @@ describe("calculatePercentageCorrect function", () => { const result = calculatePercentageCorrect(round) - expect(result).to.equal(.33) + expect(result).to.equal(33.33) }) }) @@ -145,9 +145,8 @@ describe("endRound function", () => { takeTurn(wrongGuess, round) takeTurn(correctGuess, round) - const percentCorrect = calculatePercentageCorrect(round) - const result = endRound(percentCorrect) + const result = endRound(round) - expect(result).to.equal("** Round over! ** You answered 33% of the questions correctly!") + expect(result).to.equal("** Round over! ** You answered 33.33% of the questions correctly!") }) }) \ No newline at end of file