Skip to content

Instantly share code, notes, and snippets.

@armenr
Last active December 6, 2016 17:48
Show Gist options
  • Save armenr/b7992b1b2468005f643bdf426e821da9 to your computer and use it in GitHub Desktop.
Save armenr/b7992b1b2468005f643bdf426e821da9 to your computer and use it in GitHub Desktop.
buildTriangle.js
function buildTriangle(numLines) {
var linesArray = [];
var iterations = numLines;
if (numLines < 1 ) {
return alert("Try a bigger number!")
}
for (var line = 1; line <= iterations; line++) { //start @ 1 instead of zero to avoid empty first index string
linesArray.push("#".repeat(line) + "\n");
}
return console.log(linesArray.join(""));
}
@erikyuzwa
Copy link

LGTM. I would consider maybe handling numLines with any value less then 1.
Maybe something like..

   // drop in after "var" declarations and before "for" loop
   // if our numLines param is less then 1, then return an empty string
   if (numLines < 1) { 
     return ""; 
   }

@armenr
Copy link
Author

armenr commented Dec 5, 2016

Edited. Thanks for the suggestion!

@davehallman
Copy link

davehallman commented Dec 6, 2016

Try this - I think it makes it more readable: https://jsfiddle.net/q9xywy8m/5/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment