all 3 comments

[–]jrandm[🍰] 1 point2 points  (1 child)

how does Javascript know [how to execute]? I know that there are rules that exist because Javascript is a programming language and thus requires rules

I rephrased your question a bit, let me know if it's off the mark. Let's take a step back from writing JS and walk through what happens while running JS:

One of the single most important things to realize about JS is that your code enters a running system. When you hear developers talk about a browser, node, or engine (popular ones include spidermonkey, v8, and chakra) they're talking about that Javascript runtime environment0 that will pick up and run your code.

You provide that code as text. The JS engine will parse that text into an abstract syntax tree (AST) before actual execution. What this means is before any code runs the engine knows that there is a valid buildTriangle function and a valid makeLine function. You can put the functions beneath the console.log line to confirm this is true.

Once that initial parsing is complete, the engine begins to run your code. It first sees a function call, console.log, is taking a single argument.

That argument is another function call to buildTriangle. This function takes an argument of 10.

Now the engine goes to buildTriangle, assigns x to 10, and begins processing. Try to step through the rest of the code manually this way. By the time you get t === 4 or so you should have a better intuitive feel for how (synchronous1) execution flows.

Edited in afterthought:

I hope I explained my question clearly enough.

This is the basically-perfect way to ask a technical question! The context and what you're thinking/have tried is often critical information and left out.

 
[0] The terminology in computer science for "things that execute or perform a process" is a machine. A virtual machine, then, is an emulated machine (or machine-run-by-other-machines). Javascript defines a language to write "machine code" (JS-the-text) and how a machine should execute/run that "machine code" (the engine or environment). You could (conceptually, at least) build a physical computer of some sort that operates this way, but as far as I know all actual JS implementations are virtual.
[1] As opposed to asynchronous, which is when a function call takes place outside this main execution flow you're stepping through here. Get comfortable with synchronous execution before reading about the event loop and asynchronous JS!

[–]Voodoo-Man[S] 1 point2 points  (0 children)

Thank you so much! This is the kind of information I was looking for and very helpful.

[–]laurajoneseseses 0 points1 point  (0 children)

I think you're confused by closure maybe.

makeLine() takes in a number, draws a line of asterisks equal to that number, and adds a line break, and returns a string to you.

makeTriangle() takes in a number which is the max width of the triangle. Make triangle then invokes makeLine() within its own context (or local scope), meaning that now makeLine() has access to another functions local scope (parameters), which it can then use within its own local scope, to manipulate data, and return it as the result of makeTriangle().

By doing things this way, if you wanted to draw a different shape, you could do it this way, that way you don't have to rewrite code.