Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

I am not sure how to proceed, if I try for example pyramid(“o”, 4, false), I can print out:

row 1: 3 empty spaces, 1 char
row 2: 2 empty spaces, 3 char
row 3: 1 empty space, 5 char
row 4: 0 empty spaces, 7 char

which seems like the test case, what am I missing?

Your code so far

const pyramid = (char, rows, bool) => {
    let result = [];
    let cRow = 1;

    //startMarker
    result.push("\n");

    if (bool === "false") {
        for (let j = cRow; j <= rows; j++) {

            //spacing
            for (let i = 1; i <= rows - cRow; i++) {
                result.push(" ");
            }

            //char
            for (let i = 1; i <= cRow * 2 - 1; i = i + 1) {
                result.push(char);
            }

            //endRow
            result.push("\n");
            cRow++;
        }
    }

    if (bool === "true") {
        for (let j = cRow; j <= rows; j++) {

            //spacing
            for (let i = cRow; i > 1; i--) {
                result.push(" ");
            }

            //char
            for (let i = 2 * rows - cRow; i >= cRow; i -= 1) {
                result.push(char);
                console.log(`i: ${i}, cRow: ${cRow}`);
            }

            //endRow
            console.log(`result: ${result}`);
            result.push("\n");
            cRow++;
        }
    }

    //return
    return result.join("");
}

let char = "o";
let rows = 4;
let dir = "false";
const result = pyramid(char, rows, dir);
console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.1 Safari/605.1.15

Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator

Welcome to the forum @andrened!

You can add this code to the bottom of your script file to compare what your code is producing to what is expected:

  console.log("Actual:   ", JSON.stringify(pyramid("o", 4, false)));
  console.log("Expected: ", JSON.stringify("\n   o\n  ooo\n ooooo\nooooooo\n"));
  console.log("Actual:   ", JSON.stringify(pyramid("p", 5, true)));
  console.log("Expected: ", JSON.stringify("\nppppppppp\n ppppppp\n  ppppp\n   ppp\n    p\n"));
1 Like

Boolean values are not strings in JavaScript.
You can simply write then down as false or true.

1 Like

thanks @dhess and @faust.levity I had logged spaces as “_“ to read them easier, and now the code works :trophy:

Happy to remove the code above if needed

1 Like