freeCodeCamp icon indicating copy to clipboard operation
freeCodeCamp copied to clipboard

Avoid Initial Blank Line in Pyramid Generator Output

Open umairabbasidev opened this issue 1 year ago • 10 comments

Describe the Issue

In the current implementation of the pyramid generator coding challenge, the output starts with an unnecessary blank line due to the way newline characters are added in the code. This can result in a confusing or unintended output format when generating the pyramid. The current code adds a newline character before each row, including the first row.

Affected Page

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures-v8/learn-introductory-javascript-by-building-a-pyramid-generator/step-43

Your code

const character = "#";
const count = 8;
const rows = [];

for (let i = 0; i < count; i = i + 1) {
  rows.push(i);
}

let result = "";

for (const row of rows) {
  result = result + "\n" + row; // Adds a newline before each row, causing an initial blank line
  // Suggested improvement
 result = result + row + "\n";  // Short syntax: result += row + "\n"; 
}

console.log(result);


Expected behavior

The expected behavior is for each row to be printed without a leading newline at the beginning of the output. Instead, the newline should be added after each row to separate the lines correctly. By adjusting the code to add newlines after each row, we can avoid the initial blank line, resulting in a cleaner and more readable output.

Screenshots

image

image // Suggested improvement

System

System

  • Device: Laptop
  • OS: Windows 10
  • Browser: Chrome
  • Version: 117.0.5938.132
  • The issue was encountered in both the freeCodeCamp editor and the standalone Chrome browser.
  • The output started with an unnecessary blank line in both environments.

Additional context

This issue is more of a logical improvement rather than a critical bug. Adjusting the code to avoid adding a newline before the first row can help produce cleaner and more accurate output, aligning with typical pattern generation practices. The suggested improvement is to append the newline character after each row rather than before it.

umairabbasidev avatar Sep 18 '24 07:09 umairabbasidev

I'm not sure if moving new line character from the front of pyramid to the end would make that much of difference...

gikf avatar Sep 18 '24 09:09 gikf

Hi @gikf,

I understand your point that the difference might not seem significant. However, I encountered some confusion regarding the extra newline appearing before the first row of the pyramid. After working on it, I found that moving the newline character to the end of each row resolved the issue.

Here are the two versions of my pyramid generator project:

Correct Version: Custom Pyramid Generator - This version does not have an extra newline before the first row; each row has a newline after it. Both the standard pattern and the inverted pyramid pattern do not have an extra line before the pyramid starts. Previous Version: Pyramid Generator - This version includes an extra newline before the first row. Both the standard pattern and the inverted pyramid pattern have an extra line before the pyramid starts. The updated version provides a cleaner output with the correct formatting. I hope this clarifies why I suggested the change. If you have any further questions or feedback, please let me know!

Best regards,

umairabbasidev avatar Sep 18 '24 10:09 umairabbasidev

I'd agree, it doesn't seem to make a huge difference to me either

jeremylt avatar Sep 18 '24 18:09 jeremylt

I would not say one version is more correct than the other. Both can be nitpicked for leading with new line, or trailing with it.

If anything I'd rather see skipping this more manual way of composing result and use array's join method to join rows with new line character as separator. Not necessarily to replace the current version, it could be as a refactoring at the end.

gikf avatar Sep 18 '24 19:09 gikf

I like the idea of adding a step that removes the for...of loop and replaces it with a join. Although, we already have an issue about introducing methods without a proper explanation of what a method is.

We could also just trimStart on result when logging it (or just trim I guess).

lasjorg avatar Sep 19 '24 19:09 lasjorg

Steps doing refactoring would be a terrific add, imho

jeremylt avatar Sep 19 '24 20:09 jeremylt

Hi, I'd like to fix this, could you please assign it to me

akshaywakhare avatar Oct 02 '24 16:10 akshaywakhare

Please read the contribution guide. Except for the Quizz issues, we don't assign issues.

jeremylt avatar Oct 02 '24 16:10 jeremylt

Also, there is no fix that has been decided upon, or if one is needed.

jeremylt avatar Oct 02 '24 16:10 jeremylt

This seems like a simple enough change to avoid some potential confusion. I think it's fine to open this for contribution.

A PR which resolves this issue should:

  • Update the instructions to read Use a second addition operator to append a new line after the existing result value and the added row value.
  • Update the tests to account for this change
  • Update the seed code in future steps to reflect this change.

Expanding the project to include additional refactoring steps should be explored in a separate issue.

naomi-lgbt avatar Oct 04 '24 00:10 naomi-lgbt

could you explain what the second and third changes entail? like

  • is there a separate test suite that needs updating, or is it just that regex like pattern in the hints?
  • this one a bit like what code would need changing?

AnarchistHoneybun avatar Oct 04 '24 16:10 AnarchistHoneybun

the code for the hints need to be updated to match the change, yes

the seed code for all subsequent steps need to be changed to account for this change

majestic-owl448 avatar Oct 04 '24 17:10 majestic-owl448