Skip to content

Instantly share code, notes, and snippets.

View Cst2989's full-sized avatar
🎯
Focusing

Neciu Aurel Dan Cst2989

🎯
Focusing
View GitHub Profile
@Cst2989
Cst2989 / finished.js
Created October 22, 2021 10:46
finished bowling score
export const bowlingScore = (inputRolls) => {
if (inputRolls.length < 20 || inputRolls.length > 21 || inputRolls.some(roll => roll < 0)) {
return 'Invalid rolls input'
}
const arrayOfRolls = listToMatrix(inputRolls);
return totalScore(arrayOfRolls);
};
@Cst2989
Cst2989 / list-to-matrix.js
Created October 22, 2021 10:46
list to matrix
const listToMatrix = (list) => {
let matrix = [], i, k;
for (i = 0, k = -1; i < list.length; i++) {
if (i % 2 === 0) {
k++;
matrix[k] = [];
}
matrix[k].push(list[i]);
}
return matrix;
@Cst2989
Cst2989 / strike-2.js
Last active October 23, 2021 10:11
strike
if (roundScore === 'X') {
// handle Strike
const futurePoint = frames[index + 2] ? frames[index + 2][0] : 10;
const nextPoint = frames[index + 1][1] || frames[index + 1][0] !== 10
? frames[index + 1][1]
: futurePoint;
if (nextPoint) {
roundScore = 10 + frames[index + 1][0] + nextPoint;
} else {
roundScore = 10 + frames[index + 1][0];
@Cst2989
Cst2989 / double-strike.test.js
Created October 22, 2021 10:44
double stirke
test('it should return total score when we have consecutive strikes', () => {
const scoreCard = '/2XX2222/2'
const totalScore = totalScore(scoreCard);
expect(totalScore).toBe(67);
});
@Cst2989
Cst2989 / strike.js
Created October 22, 2021 10:41
check for a strike
if (roundScore === 'X') {
score += 10 + frames[index + 1][0] + frames[index + 1][1];
}
@Cst2989
Cst2989 / total-score.js
Created October 22, 2021 10:40
total score
const totalScore = (scoreCard, frames) => {
let score = 0;
scoreCard.forEach((roundScore, index) => {
if(roundScore === '/') {
score += 10 + frames[index + 1][0];
} else {
score += parseInt(roundScore)
}
})
@Cst2989
Cst2989 / total-score.test.js
Created October 22, 2021 10:34
total score test
describe('totalScore function', () => {
test('it should return total score when no spares/strike', () => {
const scoreCard = '2222222222'
const totalScore = totalScore(scoreCard);
expect(totalScore).toBe(20);
});
test('it should return total score when we have spares', () => {
const scoreCard = '/2222222/2'
const totalScore = totalScore(scoreCard);
@Cst2989
Cst2989 / bowlingScoreCard-2.js
Last active October 22, 2021 10:27
finished version
export const bowlingScoreCard = (frames) => {
let scoreCard = '';
frames.forEach((frameRolls) => {
const frameResult = calculateFrame(frameRolls[0], frameRolls[1]);
scoreCard += frameResult;
});
return scoreCard;
};
@Cst2989
Cst2989 / bowlingScorecard.test.js
Last active October 22, 2021 10:58
Bowling score card test
describe('bowlingScoreCard function', () => {
test('it should correct scorecard when no strikes/spares', () => {
const frames = [
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
[1, 1],
@Cst2989
Cst2989 / bowlingScoreCard.js
Created October 22, 2021 10:23
bowlingScoreCard
const bowlingScoreCard = (frames) => {
let scoreCard = '';
return scoreCard
}