Last active
January 12, 2017 15:54
-
-
Save jsstrn/8a94aacc7efa3c92d380631ecf75211d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const Square = () => { | |
const createLength = (length) => { | |
return 'x'.repeat(length) | |
} | |
const createHeight = (length) => { | |
return 'x' + ' '.repeat(length - 2) + 'x' | |
} | |
const createArray = (length) => { | |
return Array.from('x'.repeat(length)) | |
} | |
const isFirstOrLastElement = (index, last) => { | |
return (index === 0 || index === last) | |
} | |
const createLengthAndHeight = (element, index, array) => { | |
if (isFirstOrLastElement(index, array.length - 1)) { | |
return createLength(array.length) | |
} else { | |
return createHeight(array.length) | |
} | |
} | |
const createSquare = (length) => { | |
return createArray(length) | |
.map(createLengthAndHeight.bind(this)) | |
} | |
const drawSquare = (length) => { | |
return createSquare(length).join('\n') | |
} | |
return { drawSquare } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Square { | |
constructor () { | |
this.size = 0 | |
} | |
createLength (length) { | |
return 'x'.repeat(length) | |
} | |
createHeight (length) { | |
return 'x' + ' '.repeat(length - 2) + 'x' | |
} | |
createArray (length) { | |
return Array.from('x'.repeat(length)) | |
} | |
isFirstOrLastElement (index, last) { | |
return (index === 0 || index === last) | |
} | |
createLengthAndHeight (element, index, array) { | |
if (this.isFirstOrLastElement(index, array.length - 1)) { | |
return this.createLength(array.length) | |
} else { | |
return this.createHeight(array.length) | |
} | |
} | |
createSquare (length) { | |
return this.createArray(length) | |
.map(this.createLengthAndHeight.bind(this)) | |
} | |
drawSquare (length) { | |
return this.createSquare(length).join('\n') | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const square = require('../square') | |
describe('Square should', () => { | |
it('draw a square of length 2', () => { | |
expect(square.drawSquare(2)).toEqual('xx\nxx') | |
}) | |
it('draw a square of length 3', () => { | |
expect(square.drawSquare(3)).toEqual('xxx\nx x\nxxx') | |
}) | |
it('draw a square of length 4', () => { | |
expect(square.drawSquare(4)).toEqual('xxxx\nx x\nx x\nxxxx') | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment