Skip to content
Snippets Groups Projects

Resolve "Diamond kata TS"

Merged Arnaud FREISMUTH requested to merge 115-diamond-kata-ts into master
All threads resolved!
Files
11
+ 81
0
const letters = [
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z',
];
const getDiamond = (letter: string) => {
if (letter === 'A') {
return getPyramid(letter);
}
const topPyramid = getPyramid(letter);
const bottomPyramid = topPyramid.slice(0, -1).reverse();
return [...topPyramid, ...bottomPyramid];
};
const getSpace = (numberOfSpace: number) => ' '.repeat(numberOfSpace);
const getLine = (index: number, letterIndex: number) => {
const currentLetter = letters[index];
const aroundSpace = letterIndex - index;
if (currentLetter === 'A') {
return getSpace(aroundSpace) + currentLetter + getSpace(aroundSpace);
}
const insideSpace = index * 2 - 1;
return getSpace(aroundSpace) + currentLetter + getSpace(insideSpace) + currentLetter + getSpace(aroundSpace);
};
const getPyramid = (givenLetter: string) => {
const letterIndex = letters.indexOf(givenLetter);
const pyramid = [];
for (let index = 0; index <= letterIndex; index++) {
const line = getLine(index, letterIndex);
pyramid.push(line);
}
return pyramid;
};
describe('Diamond', () => {
it('Should be A for A', () => {
expect(getDiamond('A')).toStrictEqual(['A']);
});
it('Should get B diamond', () => {
expect(getDiamond('B')).toStrictEqual([' A ', 'B B', ' A ']);
});
it('Should get C diamond', () => {
expect(getDiamond('C')).toStrictEqual([' A ', ' B B ', 'C C', ' B B ', ' A ']);
});
it('Should get A pyramid', () => {
expect(getPyramid('A')).toStrictEqual(['A']);
});
it('Should get C pyramid', () => {
expect(getPyramid('C')).toStrictEqual([' A ', ' B B ', 'C C']);
});
});
Loading