Skip to main content

Posts

Showing posts with the label books

Solutions: Object Oriented JavaScript: 3. Functions

Previous: Chapter 2: Primitives Chapter 3: Functions 1. Write a function that converts a hexadecimal color, for example blue "#0000FF", into its RGB representation "rgb(0,0,255)". Name your function getRGB() and this it with this code:   var a = getRGB("#00FF00");   a;          rgb(0,255,0)   //Simple logic for getRgb()     var getRGB = function f(hexColor) {     var result = "rgb(";        result += parseInt("0x"+hexColor[1]+hexColor[2]) + ", ";     result += parseInt("0x"+hexColor[3]+hexColor[4]) + ", ";     result += parseInt("0x"+hexColor[5]+hexColor[6]) + ")";     return result;   }       >> getRGB("#0000FF");   "rgb(0, 0, 255)"   >> getRGB("#00FF00");   "rgb(0, 255, 0)" 2. What does each of these lines print in the console?     >> parseInt(1e1)     10    //1e1 is evaluated first     >> parseInt('1e1'

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 "