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

Iteration3 #839

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
19 changes: 18 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
// This is where your project starts.
console.log('Your project is running...');
// startGame.js
const card = require('./src/Card.js');
const deck = require('./src/Deck.js');
const round = require('./src/round.js');
const game = require('./src/game.js');
const data = require('./src/data.js');
const util = require('./src/util.js');
const prototypeQuestions = data.prototypeData;

console.log('Your project is running...');
function start() {
const cards = prototypeQuestions.map((question) => {
return card.createCard(question.id, question.question, question.answers, question.correctAnswer);
});
const deck1 = deck.createDeck(cards);
const round1 = round.createRound(deck1);
util.main(round1);
}
start();
21 changes: 21 additions & 0 deletions src/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function createCard(id, question, answers, correctAnswer) {
return {
id,
question,
answers,
correctAnswer
}

}
function evaluateGuess(guess, correctAnswer) {
if (guess === correctAnswer) {
return 'correct!';
} else {
return 'incorrect!';
}
}

module.exports = {
createCard,
evaluateGuess,
}
13 changes: 13 additions & 0 deletions src/deck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function createDeck(cards) {
return {
cards,
numberOfCards: cards.length
};
}
function countCards(deck) {
return deck.numberOfCards;
}
module.exports = {
createDeck,
countCards
}
8 changes: 4 additions & 4 deletions src/game.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const data = require('./data');
const data = require('./data.js');
const prototypeQuestions = data.prototypeData;
const util = require('./util');

const util = require('./util.js');
const decks = require('./Deck.js');
function printMessage(deck) {
console.log(`Welcome to FlashCards! You are playing with ${countCards(deck)} cards.
console.log(`Welcome to FlashCards! You are playing with ${deck.countCards(deck)} cards.
-----------------------------------------------------------------------`);
}

Expand Down
41 changes: 41 additions & 0 deletions src/round.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function createRound(deck) {
return {
deck,
currentCard: 0,
turns: 0,
incorrectGuesses: [],
takeTurn(guess) {
this.turns++;
const currentCard = this.deck.cards[this.currentCard];
if (guess === currentCard.correctAnswer) {
// Move to the next card
this.currentCard++;
return 'correct!';
} else {
// Store the incorrect guess (card id) in the array
this.incorrectGuesses.push(currentCard.id);

// Move to the next card
this.currentCard++;
return 'incorrect!';
}
},
}
};

function calculatePercentCorrect(round) {
const correctGuesses = round.turns - round.incorrectGuesses.length;
const percentCorrect = (correctGuesses / round.turns) * 100 || 0;
return percentCorrect.toFixed(2);
}

function endRound(round) {
const percentCorrect = calculatePercentCorrect(round);
return `** Round over! ** You answered ${percentCorrect}% of the questions correctly!`
}

module.exports = {
createRound,
calculatePercentCorrect,
endRound
}
40 changes: 34 additions & 6 deletions test/card-test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
const chai = require('chai');
const expect = chai.expect;

const { createCard } = require('../src/card');
const { createCard, evaluateGuess } = require('../src/Card');

describe('card', function() {
it.skip('should be a function', function() {
let card;

beforeEach(function() {
card = createCard(1, 'What allows you to define a set of related information using key-value pairs?', ['object', 'array', 'function'], 'object');
});

it('should be a function', function() {
expect(createCard).to.be.a('function');
});

it.skip('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');

it('should create a card and its properties', function() {
expect(card.id).to.equal(1);
expect(card.question).to.equal('What allows you to define a set of related information using key-value pairs?');
expect(card.answers).to.deep.equal(['object', 'array', 'function']);
expect(card.correctAnswer).to.equal('object');
});
});
});

describe('evaluateGuess', function() {
let card;

beforeEach(function() {
card = createCard(1, 'What allows you to define a set of related information using key-value pairs?', ['object', 'array', 'function'], 'object');
});

it('should be a function', function() {
expect(evaluateGuess).to.be.a('function');
});

it('should return correct if guess is correct', function() {
const guess = 'object';
const feedback = evaluateGuess(guess, card.correctAnswer);
expect(feedback).to.equal('correct!');
});

it('should return incorrect if guess is incorrect', function() {
const guess = 'array';
const feedback = evaluateGuess(guess, card.correctAnswer);
expect(feedback).to.equal('incorrect!');
});
});
29 changes: 29 additions & 0 deletions test/deck-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const chai = require('chai');
const expect = chai.expect;

const { createCard } = require('../src/Card');
const { createDeck, countCards } = require('../src/Deck');

describe('deck', function() {
let card1, card2, card3, cards, deck;

beforeEach(function() {
card1 = createCard(1, 'What allows you to define a set of related information using key-value pairs?', ['object', 'array', 'function'], 'object');
card2 = createCard(2, 'What is a comma-separated list of related values?', ['array', 'object', 'function'], 'array');
card3 = createCard(3, 'What type of prototype method directly modifies the existing array?', ['mutator method', 'accessor method', 'iteration method'], 'mutator method');
cards = [card1, card2, card3];
deck = createDeck(cards);
});

it('should be a function', function() {
expect(createDeck).to.be.a('function');
});

it('should create a deck and its properties', function() {
expect(deck.cards).to.deep.equal(cards);
expect(deck.numberOfCards).to.equal(3);
});
it('should be able to count the cards in the deck', function() {
expect(countCards(deck)).to.equal(3);
});
});
43 changes: 43 additions & 0 deletions test/round-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const chai = require('chai');
const expect = chai.expect;

const { createCard } = require('../src/Card');
const { createDeck } = require('../src/Deck');
const { createRound, calculatePercentCorrect, endRound } = require('../src/round');

describe('round', function() {
let card1, card2, card3, cards, deck, round;

beforeEach(function() {
card1 = createCard(1, 'What allows you to define a set of related information using key-value pairs?', ['object', 'array', 'function'], 'object');
card2 = createCard(2, 'What is a comma-separated list of related values?', ['array', 'object', 'function'], 'array');
card3 = createCard(3, 'What type of prototype method directly modifies the existing array?', ['mutator method', 'accessor method', 'iteration method'], 'mutator method');
cards = [card1, card2, card3];
deck = createDeck(cards);
round = createRound(deck);
});

it('should start a round', function() {
expect(round.deck).to.deep.equal(deck);
expect(round.currentCardIndex).to.equal(0);
expect(round.turns).to.equal(0);
expect(round.incorrectGuesses).to.deep.equal([]);
});

it('should calculate the percent correct', function() {
round.takeTurn('object');
round.takeTurn('array');
round.takeTurn('mutator method');
const percentCorrect = calculatePercentCorrect(round);
expect(percentCorrect).to.equal('100.00');
});

it('should end the round', function() {
round.takeTurn('object');
round.takeTurn('array');
round.takeTurn('mutator method');
const percentCorrect = calculatePercentCorrect(round);
const end = endRound(round);
expect(end).to.equal(`** Round over! ** You answered ${percentCorrect}% of the questions correctly!`);
});
});