Welcome to Day 2 of our challenge series. Today, you’ll be using some clean and polished code to create a clean and polished diamond.
Our challenge comes from user @jayeshcp on CodeWars.
Your task is to return a string that displays a diamond shape on the screen using asterisk (“*”) characters.
The shape that the print method will return should resemble a diamond. A number provided as input will represent the number of asterisks printed on the middle line. The line above and below will be centered and will have two less asterisks than the middle line. This reduction will continue for each line until a line with a single asterisk is printed at the top and bottom of the figure.
Return
null
if input is an even number or a negative number.Note: JS and Python students must implement
diamond()
method and returnNone
(Py) ornull
(JS) for invalid input.
Bonus points awarded for experimenting with any extra features.
Good luck!
Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (85)
CSS
Some people will claim that I am cheating with this one; and they are probably right... but it was fun to develop and it kind of works (although only with odd numbers 😭). The idea is:
Here there is a working demo on Codepen:
Wow amazing one again! I don't think it's cheating at all, just a creative solution to the problem!
Thanks!
Yesterday's solution was definitely more cheating than this one.
Amazing! You are a CSS master!
Good job! :)
After @andrewbrown shamed us all yesterday for not having test cases, I decided I needed to step up my game and went full TDD with this one!
Rust Solution:
+1 for Rust
+1 for TDD
Nice!
Hey! Also JavaScript
This is my favorite JS answer to this challenge 👍 I like how you used the repeater, keeps things clean & compact.
Haskell!
Go (with bonus diamond of diamonds) playground link
Woah that's cool! I wanted to see what your diamond of diamonds looked like!
Thought I'd paste in the medium sized one, and let people go to the playground for the big one!
JS ❤️
BASH
Ruby
I didn’t follow the fine print so I’m not sure this totally fits the spec now that I’m reading more carefully.
I’ll be more careful tomorrow 😄
Python
The shortest one by far
It's also fast.
Ruby solution