freeCodeCamp / CurriculumExpansion

Creative Commons Attribution Share Alike 4.0 International
313 stars 105 forks source link

chore: convert pyramid generator project to lab #567

Closed jdwilkin4 closed 1 week ago

jdwilkin4 commented 3 weeks ago

We currently have this pyramid generator project in the beta JS curriculum. https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures-v8/learn-introductory-javascript-by-building-a-pyramid-generator/step-118

the reason why it has so many steps because it is currently being used to teach all of the JS fundamentals like functions, strings, arrays, variables , etc.

But with the new frontend cert, we already have plenty of smaller workshops and labs to teach JS fundamentals. So we are going to convert this project into a lab.

here is the current solution:

const character = "#";
const count = 8;
const rows = [];
let inverted = true;

function padRow(rowNumber, rowCount) {
  return " ".repeat(rowCount - rowNumber) + character.repeat(2 * rowNumber - 1) + " ".repeat(rowCount - rowNumber);
}

for (let i = 1; i <= count; i++) {
  if (inverted) {
    rows.unshift(padRow(i, count));
  } else {
    rows.push(padRow(i, count));
  }
}

let result = ""

for (const row of rows) {
  result = result + "\n" + row;
}

console.log(result);

You don't need to keep the global variables. You are free to just create a function that will generate the pyramid with a character, count and isInverted boolean. Or you can have two functions if you want. One for the padRow and one to generate the pyramid. It is up to you.

Play around with the current solution and see how you can refactor this to convert it to a lab. Then add your user stories.