Appendix E. Answers to Exercise Questions

This appendix lists possible answers to the exercises at the end of the chapters. Possible answers meaning they are not the only ones, so don't worry if your solution is different.

As with the rest of the book, you should try them in your console and play around a bit.

The first and the last chapters don't have the Exercises section, so let's start with Chapter 2, Primitive Data Types, Arrays, Loops, and Conditions.

Chapter 2, Primitive Data Types, Arrays, Loops, and Conditions

Lets try and solve the following exercises:

Exercises

  1. The result will be as follows:
            > var a; typeof a; 
            "undefined" 
    

    When you declare a variable but do not initialize it with a value, it automatically gets the undefined value. You can also check:

            > a === undefined; 
            true 
    

    The value of v will be:

            > var s = '1s'; s++; 
            NaN 
    

    Adding 1 to the string '1s' returns the string '1s1', which is Not A Number, but the ++ operator should return a number; so it returns the special NaN number.

    The program is as follows:

            > !!"false"; 
            true 
    

    The tricky part of the question is that "false" is a string and all strings are true when cast to Booleans (except the empty string ""). If the question wasn't about the string "false" but the Boolean false instead, the double negation !! returns the same Boolean:

            > !!false; 
            false 
    

    As you'd expect, single negation returns the opposite:

            > !false; 
            true 
            > !true; 
            false 
    

    You can test with any string and it will cast to a Boolean true, except the empty string:

            > !!"hello"; 
            true 
            > !!"0"; 
            true 
            > !!""; 
            false 
    

    The output after executing undefined is as follows:

            > !!undefined; 
            false 
    

    Here undefined is one of the falsy values and it casts to false. You can try with any of the other falsy values, such the empty string "" in the previous example, NaN, or 0.

            > typeof -Infinity; 
            "number" 
    

    The number type includes all numbers, NaN, positive and negative Infinity.

    The output after executing the following is:

            > 10 % "0"; 
            NaN 
    

    The string "0" is cast to the number 0. Division by 0 is Infinity, which has no remainder.

    The output after executing the following is:

            > undefined == null; 
            true 
    

    Comparison with == operator doesn't check the types, but converts the operands; in this case both are falsy values. Strict comparison checks the types too:

            > undefined === null; 
            false 
    

    The following is the code line and its output:

            > false === ""; 
            false 
    

    Strict comparison between different types (in this case Boolean and string) is doomed to fail, no matter what the values are.

    The following is the code line and its output:

            > typeof "2E+2"; 
            "string" 
    

    Anything in quotes is a string, even though:

            > 2E+2; 
            200 
            > typeof 2E+2; 
            "number" 
    

    The following is the code line and its output:

            > a = 3e+3; a++; 
            3000 
    

    3e+3 is 3 with three zeroes, meaning 3000. Then ++ is a post-increment, meaning it returns the old value and then it increments it and assigns it to a. That's why you get the return value 3000 in the console, although a is now 3001:

            > a; 
            3001 
    
  2. The value of v after executing the following is:
            > var v = v || 10; 
            > v; 
            10 
    

    If v has never been declared, it's undefined so this is the same as:

            > var v = undefined || 10; 
            > v; 
            10 
    

    However, if v has already been defined and initialized with a non-falsy value, you'll get the previous value.

            > var v = 100; 
            > var v = v || 10; 
            > v; 
            100 
    

    The second use of var doesn't "reset" the variable.

    If v was already a falsy value (not a 100), the check v || 10 will return 10.

            > var v = 0; 
            > var v = v || 10; 
            > v; 
            10 
    
  3. For printing multiplication tables, perform the following:
            for (var i = 1; i <= 12; i++) { 
              for (var j = 1; j <= 12; j++) { 
                console.log(i + ' * ' + j + ' = ' + i * j); 
              } 
            } 
    

    Or:

            var i = 1, j = 1; 
            while (i <= 12) { 
              while (j <= 12) { 
                console.log(i + ' * ' + j + ' = ' + i * j); 
                j++; 
              } 
              i++; 
              j = 1; 
            } 
    
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.237.136