Build a Pyramid Generator - Build a Pyramid Generator

Tell us what’s happening:

all answers are accurate guys but can’t pass the test

Your code so far

function pyramid(string, integer, boolean) {
  let empty = [];
  if (boolean === false) {
    for (let i = 1; i <= integer; i++) {
      empty.push('\n ', string.repeat(i * 1.8));
    }
  } else if (boolean === true) {
    for (let i = integer; i >= 1; i--) {
      empty.push('\n ', string.repeat(i * 1.8));
    }
  }
  return empty.join('');
}
console.log(pyramid('o', 5, false))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 OPR/124.0.0.0

Challenge Information:

Build a Pyramid Generator - Build a Pyramid Generator

Your returned strings do not match at the end.

i fixed to this but still not passing

function pyramid(string, integer, boolean) {

  let empty = \[\];

  let count = 9;

  if (boolean === false) {

    for (let i = 1; i <= integer; i++) {

      count--;

      empty.push(string.repeat(i \* 1.9)+'\\n'+' '.repeat(count));

    empty.unshift('\\n',' '.repeat(count + 1))



    }

  } else if (boolean === true) {

    for (let i = integer; i >= 1; i--) {

      empty.push(string.repeat(i \* 1.9));

    } 

  } 

  return empty.join('');

}

console.log(pyramid('o', 4, false))

Here are some troubleshooting steps you can follow. Focus on one test at a time:

  1. Are there any errors or messages in the console?
  2. What is the requirement of the first failing test?
  3. Check the related User Story and ensure it’s followed precisely.
  4. What line of code implements this?
  5. What is the result of the code and does it match the requirement? (Write the value of a variable to the console at that point in the code if needed.)

If this does not help you solve the problem, please reply with answers to these questions.

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add the backticks.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Are you comparing the output character for character? Every newline needs to be in the exact same place as the test output requires.

no.4 is not passing.

function pyramid(string, integer, boolean) {
  let empty = [];
  let count = integer;
  let addOne = 0;
  if (boolean === false) {
    for (let i = 0; i <= integer; i++) {
      count--;
      let safe = Math.max(0, count);
      empty[i] = string.repeat(i * 2 - 0.1) + '\n' + ' '.repeat(safe);
    }
  } else if (boolean === true) {
    for (let i = integer; i >= 0; i--){
    addOne++;
    let safeRange = Math.max(i * 2 + 1, 0 )
    empty[addOne] = string.repeat(safeRange)+'\n'+' '.repeat(addOne); 
    } 
  }
  return empty.join('');
}
console.log(pyramid('o', 5, true))

Ok but did you try my suggestion?

sure, but only no.3 passed

Ok, what did you see when you compared the expected output for test 4 character for character to your output?

i see 0 index is empty on test4

I am not sure what that means?

i try to add ‘\n’ at index 0 but nothing seem to work. this is the error i get. pyramid("p", 5, true) should return "\nppppppppp\n ppppppp\n ppppp\n ppp\n p\n"


else if (boolean === true) {
    for (let i = integer; i >= 0; i--){
    addOne++;
    let safeRange = Math.max(i * 2 + 1, 0 )
    empty[addOne] = string.repeat(safeRange)+'\n'+' '.repeat(addOne); 
    } 
    empty[0] = '\n';
  }

Just clobbering whatever is at the beginning can’t work, then you code would not exactly match the specified output. Is there another way to put strings together?

i get it now it returned false, i’m working on it

1 Like