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/final iteration #863

Open
wants to merge 4 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
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// This is where your project starts.
// const {prototypeData} = require('./src/data')
const {startGame} = require('./src/game')

startGame()

console.log('Your project is running...');



22 changes: 22 additions & 0 deletions src/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function createCard(id, question, answers, correctAnswer){
card = {
id: id,
question: question,
answers: answers,
correctAnswer: correctAnswer
}
return card
}

function evaluateGuess(guess, correctAnswer){
if (guess === correctAnswer){
return 'Correct!'
} else {
return 'Incorrect :('
}
}

module.exports = {
createCard,
evaluateGuess
}
16 changes: 15 additions & 1 deletion src/game.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const data = require('./data');
const prototypeQuestions = data.prototypeData;
const util = require('./util');
const { createDeck, countCards, createRound, takeTurn, calculatePercentCorrect, endRound } = require('./round')


function printMessage(deck) {
console.log(`Welcome to FlashCards! You are playing with ${countCards(deck)} cards.
Expand All @@ -11,4 +13,16 @@ function printQuestion(round) {
util.main(round);
}

module.exports = { printMessage, printQuestion };
function startGame(deck) {
var deck = createDeck(prototypeQuestions)
var round = createRound(deck)
printMessage(deck)
printQuestion(round)
}


module.exports = {
printMessage,
printQuestion,
startGame
};
54 changes: 54 additions & 0 deletions src/round.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const {createCard, evaluateGuess} = require('./card')

function createDeck(cards){
deck = cards
return deck
}

function countCards(deck){
return deck.length
}

function createRound(deck) {
round = {
deck: deck,
currentCard: deck[0],
turns: 0,
incorrectGuesses: []
}
return round
}

function takeTurn(guess, round){
var response = evaluateGuess(guess, round.currentCard.correctAnswer)
round.turns ++
round.currentCard = round.deck[round.turns]
if (response === 'Incorrect :('){
round.incorrectGuesses.push(round.deck.id)
}
return response
}

function calculatePercentCorrect(round) {
if (round.turns === 0) {
return 0;
}
let percentage = ((round.turns - round.incorrectGuesses.length) / round.turns) * 100;
return percentage;
}

function endRound(round) {
const percentageCorrect = calculatePercentCorrect(round);
console.log(`**ROUND OVER** You answered ${percentageCorrect}% of the questions correctly!`)
return `**ROUND OVER** You answered ${percentageCorrect}% of the questions correctly!`
}


module.exports = {
createDeck,
countCards,
createRound,
takeTurn,
calculatePercentCorrect,
endRound
}
149 changes: 144 additions & 5 deletions test/card-test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,158 @@
const chai = require('chai');
const expect = chai.expect;

const { createCard } = require('../src/card');
const { createCard, evaluateGuess,} = require('../src/card');
const { createDeck, countCards, createRound, takeTurn, calculatePercentCorrect, endRound } = require('../src/round')

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);
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('turn', function() {
it('should be a function', function() {
expect(evaluateGuess).to.be.a('function');
})
it('should return correct when guess is correct', function() {
const guess = "array"
const correctAnswer = "array"
const result = evaluateGuess(guess, correctAnswer)
expect(result).to.equal('Correct!')
})
it('should return incorrect if the guess is not the correct answer', function() {
const guess = "object"
const correctAnswer = "array"
const result = evaluateGuess(guess, correctAnswer)
expect(result).to.equal("Incorrect :(")
})
})

describe('deck', function() {
it('should be a function', function(){
expect(createDeck).to.be.a('function')
})
it('should return an array', function(){
const card1 = createCard(1, "What allows you to define a set of related information using key-value pairs?", ["object", "array", "function"], "object")
const card2 = createCard(2, "What is a comma-separated list of related values?", ["array", "object", "function"], "array")
const card3 = createCard(3, "What type of prototype method directly modifies the existing array?", ["mutator method", "accessor method", "iteration method"], "mutator method")
const cards = [card1, card2, card3]
const result = createDeck(cards)
expect(result).to.be.an('array')
})
it('should count cards in the deck', function() {
const card1 = createCard(1, "What allows you to define a set of related information using key-value pairs?", ["object", "array", "function"], "object")
const card2 = createCard(2, "What is a comma-separated list of related values?", ["array", "object", "function"], "array")
const card3 = createCard(3, "What type of prototype method directly modifies the existing array?", ["mutator method", "accessor method", "iteration method"], "mutator method")
const deck = [card1, card2, card3]
const numCards = countCards(deck)
expect(numCards).to.equal(3)
})
})

describe('rounds', function() {
it('should be a function', function() {
expect(createRound).to.be.a('function')
})
it('should create an object', function() {
const round = createRound(deck)
expect(round).to.be.an('object')
})
it('current card should start as the first card in the deck', function() {
const round = createRound(deck)
expect(round.currentCard).to.equal(deck[0])
})
it('should initialize turns to 0', function() {
const round = createRound(deck);
expect(round.turns).to.equal(0);
});
})

describe('takeTurn', function(){
let round;
beforeEach(function() {
round = {
turns: 0,
incorrectGuesses: [],
deck: {
id: 1,
correctAnswer: 5
}
};
});
it('should be a function', function() {
expect(takeTurn).to.be.a('function');
});
it('should update turn count', function(){
takeTurn(3, round);
expect(round.turns).to.equal(1);
});
it('should add deck id to incorrectGuesses if guess is incorrect', function(){
takeTurn(3, round);
expect(round.incorrectGuesses).to.deep.equal([1]);
});
it('should not add deck id to incorrectGuesses if guess is correct', function(){
takeTurn(5, round);
expect(round.incorrectGuesses).to.be.empty;
});
it('should return "correct" if the guess is correct', function() {
const result = takeTurn(5, round);
expect(result).to.equal("Correct!");
});
it('should return "incorrect" if the guess is incorrect', function() {
const result = takeTurn(3, round);
expect(result).to.equal("Incorrect :(");
});
});

describe('calculatePercentCorrect', function() {
it('should return 100% when all guesses are correct', function() {
const round = {
turns: 10,
incorrectGuesses: []
};
expect(calculatePercentCorrect(round)).to.equal(100);
});
it('should return 0% when all guesses are incorrect', function() {
const round = {
turns: 10,
incorrectGuesses: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
};
expect(calculatePercentCorrect(round)).to.equal(0);
});
it('should return the correct percentage when there are some incorrect guesses', () => {
const round = {
turns: 10,
incorrectGuesses: [3, 5, 7]
};
expect(calculatePercentCorrect(round)).to.equal(70);
});
it('should return 0% when no turns are made', function() {
const round = {
turns: 0,
incorrectGuesses: []
};
expect(calculatePercentCorrect(round)).to.equal(0);
});
});

describe('end round', function() {
it('should be a function', function() {
expect(endRound).to.be.a('function')
})
it('should return the percentage correct', function() {
const round = {
turns: 10,
incorrectGuesses: [3, 5, 7]
}
expect(endRound(round)).to.equal('**ROUND OVER** You answered 70% of the questions correctly!')
})
})