Skip to content

Instantly share code, notes, and snippets.

@AlyoshaS
Created January 17, 2019 14:17
Show Gist options
  • Save AlyoshaS/4a7ef90f2c0a554438fc48f011ad3228 to your computer and use it in GitHub Desktop.
Save AlyoshaS/4a7ef90f2c0a554438fc48f011ad3228 to your computer and use it in GitHub Desktop.
// lucas testando aqui!
const getLetters = letter => {
const initial = 'a'.charCodeAt()
const final = letter.charCodeAt()
const gap = final - initial
const diamondLetters = Array.from({ length: gap + 1 })
.fill()
.map((_, key) => initial + key)
.map(charCode => String.fromCharCode(charCode))
return diamondLetters
}
const getDiamondLine = (letter, i, len) => {
// if (letter === 'a') return `${letter}`
let space = ''
let spaceBefore = ''
for (let j = 0; j < i + (i - 1); j++) {
space = space.concat(' ')
}
// console.log(len - i - 1, 'hey')
for (let j = len - i - 1; j > 0; j--) {
spaceBefore = spaceBefore.concat(' ')
}
return `${spaceBefore + letter + space + letter}`
}
const diamond = letter => {
const arr = getLetters(letter)
if (letter === 'a') return 'a'
if (letter === 'b')
return ` a
b b
a`
const length = arr.length
const initialSpaces = length
return arr
.map((letter, index) => {
if (index === 0) return `${letter}\n`
if (index === 1) return `${letter} ${letter}\n`
if (index === 2) return `${letter} ${letter}\n`
})
.join('')
}
/**
* Espaços dos lados
* 1 - 0
* 2 - 1,0
* 3 - 2,1,0
*/
/**
* Espaços do meio
* 1 - 0
* 2 - 0,1
* 3 - 0,1,3
*/
/**
* a
* b b
* a
*/
/**
* a
* b b
* c c
* a
*/
module.exports = {
diamond,
getLetters,
getDiamondLine,
}
const { diamond, getLetters, getDiamondLine } = require('./diamond.js')
describe('Diamonds', () => {
describe('getLetters', () => {
it('should return letters to be printed when given E', () => {
expect(getLetters('e')).toEqual(['a', 'b', 'c', 'd', 'e'])
})
it('should return letters to be printed when given C', () => {
expect(getLetters('c')).toEqual(['a', 'b', 'c'])
})
it('should return letters to be printed when given D', () => {
expect(getLetters('d')).toEqual(['a', 'b', 'c', 'd'])
})
it('should return letters to be printed when given F', () => {
expect(getLetters('f')).toEqual(['a', 'b', 'c', 'd', 'e', 'f'])
})
})
describe('getDiamondLine given an array [a,b,c,d]', () => {
const arrLen = 4
it('should return line for A', () => {
expect(getDiamondLine('a', 0, arrLen)).toEqual(' a')
})
it('should return line for B', () => {
expect(getDiamondLine('b', 1, arrLen)).toEqual(' b b')
})
it('should return line for C', () => {
expect(getDiamondLine('c', 2, arrLen)).toEqual(' c c')
})
it('should return line for D', () => {
expect(getDiamondLine('d', 3, arrLen)).toEqual('d d')
})
})
describe('diamond', () => {
const diamonds = {
a: `a`,
b: `
a
b b
a`.replace(/^\n/, ''),
f: ``,
}
it('should return diamon from A', () => {
expect(diamond('a')).toEqual(diamonds.a)
})
it('should return diamon from B', () => {
expect(diamond('b')).toEqual(diamonds.b)
})
// it('should return diamon from B', () => {
// expect(diamond('b')).toEqual(' a \nb b\n a ')
// })
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment