Home  >  Article  >  Web Front-end  >  Code for Heart Pattern using Javascript

Code for Heart Pattern using Javascript

Patricia Arquette
Patricia ArquetteOriginal
2024-10-18 20:39:31233browse

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

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!

Statement:
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