DEV Community

Cover image for Code for Heart Pattern using Javascript
Prince
Prince

Posted on

2

Code for Heart Pattern using Javascript

CODE

let heart = "";
let size = 6;
// Loop to print upper part of the heart
for (let i = size / 2; i <= size; i += 2) {
    for (let j = 1; j < size - i; j += 2) {//spaces on left
        heart += "  "; // Double space for consistent spacing
    }
    for (let j = 1; j <= i; j++) {  //  hearts in middle left
        heart += "❤️ ";
    }
    for (let j = 1; j <= size - i; j++) {//spaces b/w two halves
        heart += "  "; // Double space
    }
    for (let j = 1; j <= i; j++) { //heart in middle right
        heart += "❤️ ";
    }
    heart += "\n";
}
// Loop to print bottom part of the heart
for (let i = size; i >= 1; i--) {
    // Print spaces on the left side
    for (let j = i; j < size; j++) {
        heart += "  "; // Double space
    }
    for (let j = 1; j <= (i * 2) - 1; j++) { //hearts bottom part
        heart += "❤️ ";
    }
    heart += "\n";
}
console.log(heart);

Enter fullscreen mode Exit fullscreen mode

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay