Search code examples
javascriptdesign-patternscharacter

Advanced JavaScript Pattern Creation Problem


I am working on a project from my for fun, and I was asked the question "Write a program to output the letters H, T and L as created by * characters. Create four variables that store * patterns and use ONLY THOSE to output the letters."

So what I have to do is create text in the console looking like this

*          *
*          *
*          *
************
*          *
*          *
*          *

First I had to figure out how to create a vertical line stored in a variable. It took me a while, but I did it with the "/n" function. I got it, but can't figure out the rest of the problem. i ever  :

var side1 = "*********"
var side2 = "*"+ "\n" +"*"+ "\n" +"*"+ "\n" +"*"+ "\n" +"*"+ "\n" +"*"+ "\n" +"*"+ "\n" +"*"+ "\n" +"*"
console.log(side2)

If I one variable which is a horizontal line, and one that is vertical, how would I put them next to eachother?

This is really random because I keep on deleting and trying new things. Any ideas?


Solution

  • If I interpreted your question correctly, maybe something like this:

    // These are the 4 star patterns created
    const row = "* * * *\n";
    const leftColumn = "*\n";
    const middleColumn = "   *\n";
    const twoColumns = "*     *\n";
    
    // write H
    console.log(twoColumns + twoColumns + row + twoColumns + twoColumns);
    
    // write T
    console.log(row + middleColumn + middleColumn + middleColumn + middleColumn);
    
    // write L
    console.log(leftColumn + leftColumn + leftColumn + leftColumn + row);