Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat MVP #844

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// This is where your project starts.

const { start } = require('./src/game');
console.log('Your project is running...');

start()
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "OOP Flashcards Application",
"main": "index.js",
"scripts": {
"test": "mocha"
"test": "mocha",
"watch": "mocha --watch 'test/*.js'"
},
"keywords": [
"javascript",
Expand Down
13 changes: 13 additions & 0 deletions src/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const createCard = (id, question, answers, correctAnswer) => {
const card = {
id,
question,
answers,
correctAnswer
}
return card
}

module.exports = {
createCard
}
8 changes: 8 additions & 0 deletions src/deck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const createDeck = (cards) => cards

const countCards = (deck) => deck.length

module.exports = {
createDeck,
countCards
}
15 changes: 14 additions & 1 deletion src/game.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 };
48 changes: 48 additions & 0 deletions src/round.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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]

return resultofGuess
}

const calculatePercentageCorrect = (round) => {
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!`;
}

module.exports = {
createRound,
takeTurn,
calculatePercentageCorrect,
endRound
}
16 changes: 16 additions & 0 deletions src/turns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const evaluateGuess = (guess, correctAnswer) => {
if (guess) {
guess = guess.toLowerCase()
if (guess == correctAnswer) {
return `Correct!`
} else {
return `Incorrect!`
}
} else {
return `Please choose an answer`
}
}

module.exports = {
evaluateGuess
}
5 changes: 2 additions & 3 deletions test/card-test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
28 changes: 28 additions & 0 deletions test/deck-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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)
})
})
14 changes: 14 additions & 0 deletions test/game-test.js
Original file line number Diff line number Diff line change
@@ -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);
})
152 changes: 152 additions & 0 deletions test/round-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
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.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 result = endRound(round)

expect(result).to.equal("** Round over! ** You answered 33.33% of the questions correctly!")
})
})
33 changes: 33 additions & 0 deletions test/turns-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const chai = require ('chai');
const expect = chai.expect;
const { evaluateGuess } = require('../src/turns.js')

describe("evaluateGuess function", () => {
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("fitzGerald", "fitzgerald")

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")
})
})