Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
/*So this is a program that produces the output:

// → ___ /''''\ ______ /'\_

*/

JavaScript
//Function 1
var landscape = function () {
    var result = "";

    var flat = function ( size ) {  //function 2
        for (var count = 0; count < size ; count ++)
            result += "_";
    };

    var mountain = function ( size ) {   //function 3
        result += "/";
    
        for (var count = 0; count < size ; count ++)
            result += " '";
    
        result += "\\";
    };
    
    flat (3);
    mountain (4);
    flat (6);
    mountain (1);
    flat (1);
    return result ;
};

console.log(landscape());


What I have tried:

I'm guessing it starts at console.log...
but not sure what next...
Posted
Updated 8-May-16 19:41pm
v2
Comments
Sergey Alexandrovich Kryukov 9-May-16 1:32am    
How is your code sample related to the title of the question?
—SA

Top down usually but also based on how you create function. example Declaration function can put anywhere while expression must put on to top
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-May-16 0:13am    
Not exactly true. Assuming there are no lexical errors, it's just top down; there is no "usually". You are right, function declarations can be after the code using them, but it simply means that processing is two-phase (despite common belief that JavaScript is a pure line-by line interpreted; this is not true at all). It needs way more detailed explanation.
—SA
Please see my comment to Solution 1.

This is not so easy to explain. The processing goes in two phase, and everything goes from top to bottom.

First, something like some lexical analyses goes. If there are lexical errors, whole script fails silently. On this phase, function declarations becomes known to the script, so they can go after the code when the functions are called. Then the execution phase starts from top to bottom.

This is a delicate moment. The usage of a function object depends on how it is declared. This is my illustration:
JavaScript
try {
   b(); // surprisingly, will work
   a(); // will throw exception
} catch(e) { alert(e); }

var a = function() { alert("a!"); };
function b() { alert("b!"); }

In this example, b declared a function, it will be processed on first pass and taken into account in the execution phase. In contrast to that assignment to the function object a is a statement, which is not executed at the moment of the call a(a). At the moment of the call, a will be equal to the object undefined, and that throws an exception.

Note the use of ';' after statement and lack of it after the function definition; essentially, this is a terminator after the assignment operator (assignment to the object a). In fact, ';' can be missing or added in both cases, but I recommend to use it exactly the way I've shown; the detecting difference will be revealed if you are using more than one statement on a line, and some other cases.

It also worth noting that if you make the code shown above a string and pass it to eval, b won't work, because the lexical phase won't be performed on the string content. The rules are the same, but there is a subtle effective difference.

And finally, a note on main. It looks like you have a pretty common misconception about it. This is not a fundamental feature of any language, not even C or C-like. This is a convention on the boundary between the language and the runtime environment. Even with C/C++, you will find similar constructs like "WinMain", and so on (don't tell me that is simply hides "real" main; this is a pure convention). In many other languages, there is no such convention; it is not fundamental in programming at all.

—SA
 
Share this answer
 
Comments
Karthik_Mahalingam 9-May-16 1:27am    
Exactly.

+5
Sergey Alexandrovich Kryukov 9-May-16 1:31am    
Thank you, Karthik.
I must admit, this matter is not properly explained in most JavaScript manuals.

Oh... I completely ignored related (unwritten) question in the body of the question. It takes another answer, Solution 3.

—SA
anaQata 9-May-16 12:10pm    
1. Thank you very much Sergey Alexandrovich for your time.

2. I guess i'l have to delve deeper into lexical analyses to comprehend fully your solution.

3. "a note on main. It looks like you have a pretty common misconception about it."
I guess I do based my java background. Thanks for the correction

4. You also seem keen on conventions and code structure: "Note the use of ';' after statement and lack of it after the function definition."
Based on my java background, which JavaScript convention do you advice when using braces in if statements and for loops? Using {braces} or without? (considering I also want to go with Object Oriented JS)
for (var a = 0; a < 10; a++){
result*=a;
}
Sergey Alexandrovich Kryukov 9-May-16 12:17pm    
You are welcome. Will you accept the answer formally?

You cannot really delve more into the lexical part, because this is the internal implementation detail. But see also a section in my article: 5. Handling Lexical Errors.

No, don't use ';' after the block's '}'. But even if you do, it won't hurt.

—SA
anaQata 9-May-16 12:27pm    
I quoted you to show your keenness on code conventions:
> "Note the use of ';' after statement and lack of it after the function definition."

My question was on using blocks in #if statements and #for loops.

Do you recommend their use? considering i would like to delve deeper into OOP JavaScript?
I answered to the title of the question in Solution 2, but I ignored the unwritten question in the code sample.

If this is the complete code, nothing will happen. You simply create a function object, but don't call it.

There is a well-known pattern for calling some function just once:
JavaScript
(function landscape() {
   alert("working");
   // ...
})();

Now, note that the name landscape is never used; you can just delete it; the function can be anonymous.
The wider pattern can have several different forms; it's called IIFE:
Immediately-invoked function expression — Wikipedia, the free encyclopedia[^].

Please learn it; it's very useful.

—SA
 
Share this answer
 
Comments
anaQata 9-May-16 12:16pm    
The code was on a topic in Nested Scope. So its just a scrappy program.
I was hoping you could explain the working of the code using "the call stack" methodology? If that's not much to ask for.
I just wanted to see the complete top down process that produces the output.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900