Reputation: 45
I'm learning on Udacity and they have a quiz which I couldn't pass myself. I decided to look at the provided solution but I need to understand the process of the code (I understand some of it). Here it is:
// 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(length) {
// Let's build a huge string equivalent to the triangle
var triangle = "";
//Let's start from the topmost line
var lineNumber = 1;
for(lineNumber=1; lineNumber<=length; lineNumber++){
// We will not print one line at a time.
// Rather, we will make a huge string that will comprise the whole triangle
triangle = triangle + makeLine(lineNumber);
}
return triangle;
}
// test your code by uncommenting the following line
// Note that the function buildTriangle() must return a string
// because the console.log() accepts a string argument
console.log(buildTriangle(10));
I understand that makeLine() will create line of asterisks based on the value of length that passed to it when it gets called inside buildTriangle()
But I don't understand how this line works:
triangle = triangle + makeLine(lineNumber);
Is it not working like the line in makeLine () which is
line += "* ";
What's the difference between using +=
and triangle = triangle + makeLine(lineNumber)
? If they work same then the output should be wrong.
Also please correct me if I'm wrong in my understanding to the variable length in makeLine(length) and buildTriangle (length). Are they different variables because of different scope? Can I change the name of the variable in buildTriangle function to be something like buildTriangle (passedValueToMakeLineFunc)?
Finally it would be highly appreciated if someone imitate the JavaScript engine and described how it will handle that code step by step in plain English.
Upvotes: 3
Views: 119
Reputation: 446
someVar += "someString"
is the same as someVar = someVar + "someString"
Go ahead and replace your example with triangle += makeLine(lineNumber)
and you'll see you get the same triangle.
length
sIn this case, they are both parameters to the functions makeLine
and buildTriangle
.
var length = 4;
console.log("outer", length); // 4
function someFunction(length) {
console.log("inner", length); // 5
}
someFunction(5);
console.log("outer", length); // 4
I'm going to assume this is a little too detailed in some places and maybe not enough in others. Enjoy the story!
makeLine
function is declared (but not run yet)buildTriangle
function is declared (but not run yet)buildTriangle
function with a param length of value 10.triangle
variable we'll build out the entire triangle into, new lines and all, initialized to an empty string.for (var lineNumber=1; lineNumber<=length; lineNumber++){ ... }
and not declare it before hand.for(lineNumber=1; lineNumber<=length; lineNumber++)
lineNumber
to 1lineNumber
is <= length (10) (So up until and including 10)lineNumber
by 1 (lineNumber++
).triangle = triangle + makeLine(lineNumber);
Which just takes the current triangle
string, appends the result of makeLine(lineNumber)
to it, and assigns that value to triangle
.makeLine
function with a param length of value 1.
length
param. (Besides the 2 functions I suppose)line
to an empty string.
line
variable is declared inside the function, it is scoped there, and is not accessible outside or across function executions.for (var j = 1; j <= length; j++) { ... }
j
to 1j
is <= length (1) (So just 1 on this makeLine execution, subsequent times, 1-2, 1-3, ..., 1-10)j
by 1 (j++
).line += "* ";
which just takes the current line
string, appends a "* " and assigns that value to line
.buildTriangle
for loop iterations, we encounter our return statement and return the build out triangle
.Upvotes: 2
Reputation: 27245
what's the difference between using += and triangle = triangle + makeLine(lineNumber)
These are equivalent:
triangle = triangle + makeLine(lineNumber);
triangle += makeLine(lineNumber);
…the variable length in makeLine(length) and buildTriangle (length) ... are they different variables…
Correct.
step by step in plain English
Apologies if this is too verbose or not what you had in mind:
makeLine
and buildTriangle
(define but do not execute)buildTriangle
with a single argument (10
) to resolve value to be passed to console.log
Execution of buildTriangle:
function buildTriangle(length) {
// Let's build a huge string equivalent to the triangle
var triangle = "";
//Let's start from the topmost line
var lineNumber = 1;
for(lineNumber=1; lineNumber<=length; lineNumber++){
// We will not print one line at a time.
// Rather, we will make a huge string that will comprise the whole triangle
triangle = triangle + makeLine(lineNumber);
}
return triangle;
}
buildTriangle
with length = 10
triangle
with an initial empty string valuelineNumber
with an initial value of 1
.for
loop: set variable lineNumber
to 1
(again)for
loop condition. is lineNumber
less than or equal to length
(10)?makeLine(1)
(lineNumber is 1)Execution of makeLine:
function makeLine(length) {
var line = "";
for (var j = 1; j <= length; j++) {
line += "* ";
}
return line + "\n";
}
makeLine
with length = 1
line
and initialize to empty stringfor
loop: declare and initialize variable j
with initial value 1
for
loop condition. is j
less than or equal to length
(1)?"* "
to line
. (line
is now "* "
)j
. j
is now 2
.for
loop condition. is j
less than or equal to length
(1)?j
is 2
. condition is false. exit loop.line
value with a newline appended: ("* \n"
)(Resume buildTriangle
execution)
triangle
to its current value (empty string) plus resolved makeLine
value: triangle
is now "* \n"
lineNumber
to 2for
loop condition. is lineNumber
less than or equal to length
(10)?lineNumber
is 2
, which is less than 10
, so loop condition is true. execute loop body.makeLine(2)
(lineNumber is 2)Execution of makeLine:
makeLine
with length = 2
line
and initialize to empty stringfor
loop: declare and initialize variable j
with initial value 1
for
loop condition. is j
less than or equal to length
(2)?"* "
to line
. (line
is now "* "
)j
. j
is now 2
.for
loop condition. is j
less than or equal to length
(2)?"* "
to line
. (line
is now "* * "
)j
. j
is now 3
.for
loop condition. is j
less than or equal to length
(2)?j
is 3
. condition is false. exit loop.line
value with a newline appended: ("* * \n"
)(Resume buildTriangle
execution)
triangle
to its current value "* \n"
plus resolved makeLine
value: triangle
is now "* \n* * \n"
lineNumber
to 3for
loop condition. is lineNumber
less than or equal to length
(10)?lineNumber
is 3
, which is less than 10
, so loop condition is true. execute loop body.makeLine(3)
(lineNumber is 3)Repeat steps above until lineNumber reaches 11 and loop exits.
triangle
to caller and exit buildTriangle
. At this point the value of triangle
is:*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
console.log
with the value returned by buildTriangle
.Your example included here for reference:
// 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(length) {
// Let's build a huge string equivalent to the triangle
var triangle = "";
//Let's start from the topmost line
var lineNumber = 1;
for(lineNumber=1; lineNumber<=length; lineNumber++){
// We will not print one line at a time.
// Rather, we will make a huge string that will comprise the whole triangle
triangle = triangle + makeLine(lineNumber);
}
return triangle;
}
// test your code by uncommenting the following line
// Note that the function buildTriangle() must return a string
// because the console.log() accepts a string argument
console.log(buildTriangle(10));
Upvotes: 3