Skip to content

Commit

Permalink
Merge pull request #10 from andresautentia/christmas-solution
Browse files Browse the repository at this point in the history
feat: add new christmas-tree solution
  • Loading branch information
César Alberca authored May 7, 2024
2 parents 413b620 + 658cb6f commit e65e6f6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/christmas-tree/solutions/short.christmas-tree.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { christmasTree } from './short.christmas-tree'

describe('christmas-tree', function () {
it('should print a christmas tree with 3 rows', function () {
const tree: string = christmasTree(3)
expect(tree).toBe(' *\n' + ' ***\n' + '*****\n' + ' |')
})

it('should print a christmas tree with 5 rows', function () {
const tree: string = christmasTree(5)
expect(tree).toBe(' *\n' + ' ***\n' + ' *****\n' + ' *******\n' + '*********\n' + ' |')
})
})
25 changes: 25 additions & 0 deletions src/christmas-tree/solutions/short.christmas-tree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const christmasTree = (rows: number): string => {
let tree: string = ''
let numberSpaces: number = rows - 1
tree = generateRow(rows, tree, numberSpaces)
tree = generateTrunk(rows, tree)
return tree
}

const generateTrunk = (rows: number, tree: string): string => {
for (let i: number = 0; i < rows; i++) {
tree += i === rows - 1 ? '|' : ' '
}
return tree
}

const generateRow = (rows: number, tree: string, numberSpaces: number): string => {
for (let i: number = 0; i < rows; i++) {
for (let o: number = 0; o < rows + i; o++) {
tree += o < numberSpaces ? ' ' : '*'
}
numberSpaces--
tree += '\n'
}
return tree
}

0 comments on commit e65e6f6

Please sign in to comment.