Skip to content

Instantly share code, notes, and snippets.

@Nocks
Created August 16, 2019 09:50
Show Gist options
  • Save Nocks/f39752eeaaddfb8281be2668552c49cb to your computer and use it in GitHub Desktop.
Save Nocks/f39752eeaaddfb8281be2668552c49cb to your computer and use it in GitHub Desktop.
For this quiz, you're going to create a function called buildTriangle() that will accept an input (the triangle at its widest width) and will return the string representation of a triangle.
/*
* Programming Quiz: Build A Triangle (5-3)
*/
// creates a line of * for a given length
function makeLine(length) {
var line = "";
for (var j = 1; j <= length; j++) {
line += "* ";
}
return line + "\n";
}
// main triangle builder
function buildTriangle(widestWidth) {
if (widestWidth > 2) {
var triangleLine = "";
for (var i = 1; i < widestWidth; i++) {
triangleLine += makeLine(i);
}
return triangleLine;
} else {
return "Widest width of the triangle must be more than 2.";
}
}
console.log(buildTriangle(10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment