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

Feature/working project #847

Open
wants to merge 2 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: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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...');
66 changes: 66 additions & 0 deletions src/card.js
Original file line number Diff line number Diff line change
@@ -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,
}
24 changes: 23 additions & 1 deletion src/game.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -11,4 +33,4 @@ function printQuestion(round) {
util.main(round);
}

module.exports = { printMessage, printQuestion };
module.exports = { printMessage, printQuestion, start };
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
160 changes: 157 additions & 3 deletions test/card-test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
});



});
})
})