Skip to content
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
60 changes: 59 additions & 1 deletion getPokerHand.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,66 @@
*/
function getPokerHand(dice) {
// Напишите ваш замечательный код здесь
if (dice && dice.length !== 5) {
throw Error('Кубиков должно быть 5');
}

return 'Покер';
let counts = {};
let pairs = 0;
let kare = false;
let pocker = false;
let set = false;
for (let el of dice) {
if (typeof el !== 'number') {
throw Error('В кубике все грани являются числами');
}
if (!counts[el]) {
counts[el] = 0;
}
counts[el]++;
}

for (el in counts) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не понял зачем так сложно?

switch (counts[el]) {
case 2:
pairs++;
break;
case 3:
set = true;
break
case 4:
kare = true;
break;
case 5:
pocker = true;
break;
}
}
if (pocker) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

poker

return 'Покер';
}

if (kare) {
return 'Каре';
}

if (pairs === 1 && set) {
return 'Фулл хаус';
}

if (set) {
return 'Тройка'
}

if (pairs === 2) {
return 'Две пары'
}

if (pairs === 1) {
return 'Пара'
}

return 'Наивысшее очко';
}

module.exports = getPokerHand;
56 changes: 55 additions & 1 deletion tests/getPokerHand-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,65 @@ const assert = require('assert');
const getPokerHand = require('../getPokerHand');

describe('getPokerHand', () => {
it('should throw `Кубиков должно быть 5` for [1, 1, 1, 1]', () => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не хватает тестов, проверяющих переданные данные


try {
const actual = getPokerHand([1, 1, 1, 1]);
throw Error('shoud Throw Error');
} catch (e) {
assert.equal(e.message, 'Кубиков должно быть 5');
}
});

it('should throw `В кубике все грани являются числами` for [1, 1, 1, 1, `1`]', () => {

try {
const actual = getPokerHand([1, 1, 1, 1, '1']);
throw Error('shoud Throw Error');
} catch (e) {
assert.equal(e.message, 'В кубике все грани являются числами');
}
});

it('should return `Покер` for [1, 1, 1, 1, 1]', () => {
const actual = getPokerHand([1, 1, 1, 1, 1]);

assert.equal(actual, 'Покер');
});

// Напишите тесты на ваш замечательный код здесь
it('should return `Каре` for [1, 1, 1, 1, 2]', () => {
const actual = getPokerHand([1, 1, 1, 1, 2]);

assert.equal(actual, 'Каре');
});

it('should return `Фулл хаус` for [1, 5, 1, 1, 5]', () => {
const actual = getPokerHand([1, 5, 1, 1, 5]);

assert.equal(actual, 'Фулл хаус');
});

it('should return `Тройка` for [1, 2, 1, 3, 1]', () => {
const actual = getPokerHand([1, 2, 1, 3, 1]);

assert.equal(actual, 'Тройка');
});

it('should return `Две пары` for [1, 2, 5, 1, 2]', () => {
const actual = getPokerHand([1, 2, 5, 1, 2]);

assert.equal(actual, 'Две пары');
});

it('should return `Пара` for [1, 2, 5, 1, 3]', () => {
const actual = getPokerHand([1, 2, 5, 1, 3]);

assert.equal(actual, 'Пара');
});

it('should return `Наивысшее очко` for [1, 2, 3, 4, 5]', () => {
const actual = getPokerHand([1, 2, 3, 4, 5]);

assert.equal(actual, 'Наивысшее очко');
});
});