# Build a Pyramid Generator - Build a Pyramid Generator

**URL:** <https://forum.freecodecamp.org/t/build-a-pyramid-generator-build-a-pyramid-generator/790013>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [May 27, 2026, 3:26pm UTC](https://forum.freecodecamp.org/t/build-a-pyramid-generator-build-a-pyramid-generator/790013 "2026-05-27T15:26:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![urbaner3](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/urbaner3/32/416074_2.png) [@urbaner3](https://forum.freecodecamp.org/u/urbaner3)\
**Post date:** [May 27, 2026, 3:26pm UTC](https://forum.freecodecamp.org/t/build-a-pyramid-generator-build-a-pyramid-generator/790013/1 "2026-05-27T15:26:24Z")

</div>

### Tell us what’s happening:

JS fundamental review step 12 on May 27, 2026.  
Ans works:  
[https://gist.github.com/Urbaner3/8907111595d3a7bd9cc39d4230866ddeJS](https://gist.github.com/Urbaner3/8907111595d3a7bd9cc39d4230866ddeJS) fundamental review step 12 on May 27, 2026.  
Ans works:

> <https://gist.github.com/Urbaner3/8907111595d3a7bd9cc39d4230866dde>

It only works if I make pyramid function return and check it in the main function part. Why is that?  
It only works if I make pyramid function return and check it in the main function part. Why is that?

### Your code so far

```js
const pyramid = function (str, Nrow, bool){
  let whole = "";
  if(bool){
    for(let i = Nrow; i > 0; i= i - 1){
      whole = whole.concat("\n");
      let spaces = "";
      let line = "";
      for(let k = 0; k< Nrow-i; k= k + 1){
        spaces = spaces.concat(" ");
      }
      for(let j = 0; j< 2*i-1; j= j + 1){
        line = line.concat(str);
      }
        // spaces = spaces.slice(0,Nrow-i-1);
      // console.log(spaces, line);
      line = spaces.concat(line);
      whole = whole.concat(line);
    } 
    whole = whole.concat("\n");
    console.log(whole);
  }
  else{
    // console.log("\n");
    for(let i = 0; i< Nrow; i= i + 1){
      whole = whole.concat("\n");
      let spaces = "", line = "";
      for(let j = 0; j< Nrow-1-i; j= j + 1){
        spaces = spaces.concat(" ");
      }
      for(let j = 0; j< 2*(i+1)-1; j= j + 1){
        line = line.concat(str);
      }
      // spaces = spaces.slice(0,Nrow-i-1);
      // if (i < 2){
        // spaces = " ";
        line = spaces.concat(line);
      // }
      // console.log(line);
      whole = whole.concat(line);
    } 
    whole = whole.concat("\n");
    console.log(whole);
  }
  
  return whole;
}
let ans = "";
let check = "";
check = pyramid("o", 4, false);
console.log("case check:", check==ans);

check = pyramid("p", 5, true);
console.log("case check:", check==ans);

```

### Your browser information:

User Agent is: `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36`

### Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-v9/lab-pyramid-generator/lab-pyramid-generator)**
>
> Learn to Code — For Free

GitHub Link: [freeCodeCamp/curriculum/challenges/english/blocks/lab-pyramid-generator/66f2836c459cfb16ae76f24f.md at main · freeCodeCamp/freeCodeCamp · GitHub](https://github.com/freeCodeCamp/freeCodeCamp/blob/main/curriculum/challenges/english/blocks/lab-pyramid-generator/66f2836c459cfb16ae76f24f.md)

---

<div class="post-metadata">

**Author:** ![dhess](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dhess/32/406235_2.png) [@dhess](https://forum.freecodecamp.org/u/dhess)\
**Post date:** [May 27, 2026, 3:41pm UTC](https://forum.freecodecamp.org/t/build-a-pyramid-generator-build-a-pyramid-generator/790013/2 "2026-05-27T15:41:56Z")

</div>

Welcome to the forum @urbaner3,

Your solution works from my end.

> [@urbaner3](#):
>
> It only works if I make pyramid function return and check it in the main function part. Why is that?

I’m not sure I understand what you are asking about. Your function should return a string, which it does. And you do not need to check what it returns to pass the tests.

Happy coding

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [June 24, 2026, 3:42pm UTC](https://forum.freecodecamp.org/t/build-a-pyramid-generator-build-a-pyramid-generator/790013/3 "2026-06-24T15:42:16Z")

</div>

Closed on June 24, 2026, 3:42pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
