DEV Community

Discussion on: Daily Challenge #2 - String Diamond

Collapse
 
vinniew1rus profile image
Vinnie

Extremely late to the party but here's my solution in JS:


const diamond = (size) => {
    if (size % 2 == 0 || size < 1) return null;

    const top = [...Array(size+1).keys()].reduce((acc, item) => {
        if(item % 2 != 0) acc.push([...Array((size - item) / 2).fill(' '), ...Array(item).fill('*'), ...Array((size - item) / 2).fill(' ')]);
        return acc;
    },[]);

    return [...top, ...top.splice(0, top.length -1).reverse()].map(row => row.join('')).join('\r\n');
}