Last active
December 24, 2023 14:29
-
-
Save mcsee/1c4881a54286a827b8fc037fdd89722c to your computer and use it in GitHub Desktop.
This file contains 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
function createChristmasTree(height) { | |
let tree = ''; | |
let currentFloor = 1; | |
while (currentFloor <= height) { | |
tree += ' '.repeat(height - currentFloor) + 'π'.repeat(currentFloor) | |
+ '\n'; | |
currentFloor++; | |
} | |
return tree; | |
} | |
// The side effects are OUTSIDE the function | |
console.log(createChristmasTree(7)); | |
// You can also test it | |
const createChristmasTree = createChristmasTree(7); | |
describe('createChristmasTree', () => { | |
it('should create a Christmas tree of the specified height', () => { | |
const expectedTree = | |
' π\n' + | |
' ππ\n' + | |
' πππ\n' + | |
' ππππ\n' + | |
' πππππ\n' + | |
' ππππππ\n' + | |
'πππππππ\n'; | |
const result = createChristmasTree(7); | |
expect(result).toEqual(expectedTree); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment