Home > Web Front-end > JS Tutorial > body text

Code for Heart Pattern using Javascript

Patricia Arquette
Release: 2024-10-18 20:39:31
Original
302 people have browsed it

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);

Copy after login

The above is the detailed content of Code for Heart Pattern using Javascript. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template