Directions:
For this quiz, you're going to create a function calledbuildTriangle()
that will accept an input (the triangle at its widest width) and will return the string representation of a triangle. See the example output below.
buildTriangle(10);
Returns:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
We've given you one functionmakeLine()
to start with. The function takes in a line length, and builds a line of asterisks and returns the line with a newline character.
function makeLine(length) {
var line = "";
for (var j = 1; j <= length; j++) {
line += "* "
}
return line + "\n";
}
You will need to call thismakeLine()
function inbuildTriangle()
.
This will be the most complicated program you've written yet, so take some time_thinking_through the problem before diving into the code. What tools will you need from your JavaScript tool belt? Professionals plan out their code before writing anything. Think through the steps your code will need to take and write them down in order. Then go through your list and convert each step into actual code. Good luck!
Your Code:
/*
* Programming Quiz: Build A Triangle (5-3)
*
* 1. line을 만드는 makeLine function을 만든다. for loop, "* "를 length 만큼 만들고 return + "\n" 한다.
* 2. buildTriangle function 을 만든다. 입력받은 숫자(num)와 makeLine function을 연결하는 function.
* "* "을 length 만큼 찍고(for loop: j) 줄 바꾸는 것을, 입력받은 숫자(num)만큼 돌려(for loop: i) 완성.
*/
// creates a line of * for a given length
function makeLine(length) {
var line = "";
for (var j = 1; j <= length; j++) {
line += "* ";
}
return line + "\n";
}
// your code goes here. Make sure you call makeLine() in your own code.
function buildTriangle(num) {
var pattern = "";
for (var i = 1; i <= num; i++) {
pattern += makeLine(i);
}
return pattern;
}
// test your code by uncommenting the following line
console.log(buildTriangle(10));