Skip to main content

Solutions: Object Oriented JavaScript: Chapter 2: Primitives


JavaScript experts can skip this.  

While reading the book, Object Oriented JavaScript by Stoyan Stefanov, I thought it would be a good idea to just solve the exercises presented in the book and store the solutions away. Takes me back to my school days when I was serious about learning stuff :) This will be a multi-part blog, with each blog entry solving exercises presented in one chapter of the book. The entire set of solutions will be eventually made available in my JavaScript site. So here goes ...

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

1. What is the result of executing each of these lines in the console? Why?
>> var a; typeof a;
undefined  // a has not been defined with any value

>> var s = '1s'; s++;
NaN  // converting 1s to a number is ambiguous with addition
 
>> !!"false"
true  // "false" is a valid string
 
>> !!undefined
false  //undefined is a falsy
 
>> typeof -Infinity
"number"  //this is the largest negative number supported by JS
    
>>10 % "0"
NaN  //Reminder or modulo of a number when divided by 0 is undefined
 
>> undefined == null
true  //both are falsy and compared for only equality of value

>> false === ""
false  //compared for equality and type

>> typeof "2E+2"
string  //enclosed within quotes

>> a = 3e+3; a++;
3000  //post increment


2. What is the value of v after the following?     

    var v = v || 10;
  Experiment by first setting  to 100, 0, null, or unset it (delete v)


>> var v = v||10;
10  //v is ORed with 10

>> var v = 100; var v = v||10;
100  //v is already defined as 100. Lazy initialization kicks in

>> var v = 0; var v = v||10;
10  //10 is ORed with 0

>> var v = null; var v = v||10;
10  //null is ORed with 10

>> delete v; var v = v||10;
10  //v is redefined with then ORed with 10


3. Write a script that prints out multiplication table. 
 
>> //The code snip below prints out tables from 1 to 10
var i, j;
var result = "\n";
for(i=1; i<=10; i++) 
  for(j=1; j<=10; j++) {
    result += i + " X " + j + " = " + (i * j) + "\n";
  }
  result += "\n"; 
result;
  
"
1 X 1 = 1
1 X 2 = 2
....
1 X 10 = 10

2 X 1 = 2
2 X 2 = 4
....
 
10 X 9 = 90
10 X 10 = 100
"


Next: Chapter 3: Functions

Comments

  1. hey man,
    thanks for the solution so far...
    but where are the rest of them?
    are you planing to post them?
    thanks

    ReplyDelete
    Replies
    1. Hi Anonymous, thanks for visiting the blog. I am totally held up writing a book on jQuery Mobile right now. Hope to put up rest of the chapters soon :)

      Delete
    2. Hello, Is it possible to get answers of all the other chapters Mr. Jain?

      Delete
  2. Hello, Is it possible to get answers of all the other chapters Mr. Jain?

    ReplyDelete

Post a Comment

Popular posts from this blog

Using duplicate IDs in HTML

Well today I'm being a bit controversial. Let us see what the HTML5 spec says about unique IDs in a HTML file. The  id  attribute specifies its element's  unique identifier (ID) . The value must be unique amongst all the IDs in the element's  home subtree  and must contain at least one character. The value must not contain any  space characters . An element's  unique identifier  can be used for a variety of purposes, most notably as a way to link to specific parts of a document using fragment identifiers, as a way to target an element when scripting, and as a way to style a specific element from CSS. Yes its been mentioned almost everywhere on the planet that ID must be unique. Now let us look at the below code, Launch dup.css #p2 {   background-color: yellow;  } dup-id.html <!DOCTYPE html> <html>   <head>     <title>Duplicate ID Tester</title> <link rel="stylesheet" href="dup.css" />  

Minimal required code in HTML5

I've encountered this question repeatedly of late. "What are the tags required at bare minimum for a html file?" Earlier there were a bunch of mandatory tags that were required for any html file. At bare minimum, the recommended structure was: (ref: http://www.w3.org/TR/html4/struct/global.html ) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <HTML>   <HEAD>     <TITLE>A small HTML</TITLE>   </HEAD>   <BODY>     <P>Small HTML file!</P>   </BODY> </HTML> Yes, using capitals for the tags was the way to go! Those were the days of the purists and strict was the way to be. Now open your notepad and copy the above code, save the file as old.html and launch it in Chrome or Firefox. You will see only one line "Small HTML file!" shown. Now launch the developer tools in Chrome or Inspect Element in Firefox. Thi

Fixing Date, Time and Zone on RHEL 6 command line

Had to fix all time related issues on a remote RHEL 6 server which runs without any windowing system. Plain ol' command line. Documenting steps here for future reference: Check to see if your date and timezone settings are accurate: # date # cat /etc/sysconfig/clock The server I accessed had wrong settings for both the commands. Here are the steps I used to correct: Find out your timezone from the folder /usr/share/zoneinfo # ls /usr/share/zoneinfo Mine was pointing to America/EDT instead of  Asia/Calcutta Update and save the /etc/sysconfig/clock file to # sudo vi /etc/sysconfig/clock ZONE="Asia/Calcutta" UTC=true ARC=false Remove the /etc/localtime # sudo rm /etc/localtime Create a new soft link to your time zone # cd /etc # sudo ln -s /usr/share/zoneinfo/Asia/Calcutta /etc/localtime # ls -al localtime Now it should show the link to your time zone Set your hardware clock to UTC # sudo hwclock --systohc --utc # hwclock --show Update your t