JavaScript experts can skip this.

While reading the book,

**Object Oriented JavaScript**by*, 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 ...***Stoyan Stefanov**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)

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 += i + " X " + j + " = " + (i * j) + "\n";

}

result += "\n";

}

result;

**"**

1 X 1 = 1

1 X 2 = 2

....

1 X 10 = 101 X 1 = 1

1 X 2 = 2

....

1 X 10 = 10

2 X 1 = 2

2 X 2 = 4

....

2 X 1 = 2

2 X 2 = 4

....

**10 X 9 = 90**

hey man,

ReplyDeletethanks for the solution so far...

but where are the rest of them?

are you planing to post them?

thanks

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 :)

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

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

ReplyDelete