- Why do languages provide the switch statement, when we can achieve the same thing with multiple if... elseif statements? Show one example of how you might use the switch statement.
Answer: if/elseif strings will run through the entire sequence of statements, and if there are a lot of options, it could add a lot of time to the running of your program. Switch statements have different “cases” and the thread jumps right to the appropriate choice and then gets out of the loop with a “break”. For an instance with many cases, switch is the best choice.
EX:
var gameName = monopoly
switch (gameName) {
case “scrabble”:
alert (“scrabble is a word game”);
break;
case “chess”:
alert (“chess is a strategy game”);
break;
case “monopoly”:
alert (“monopoly is a business game”);
break;
default:
alert (“unknown game”);
break;
}
- What is encapsulation, and what do functions encapsulate?
Answer: encapsulation is when Information is privately seen by a function (information that can not be changed from outside the function)
- What is a pure function? Is the function show() provided in Eloquent Javascript a pure function?
Answer: a pure function has one input and one output and is one that does not change anything outside of the function or, rather, has no side effects. Show() has side effects -- it writes text to the screen and requires an output text box to function properly
- What do we mean when we say a variable in a function is shadowing a top level variable?
Answer: Variables declared in functions are unique to that function. This means that functions can reuse the name of a variable from the program without effecting the original variable’s value. This is called shadowing.
- A recursive function, must have some sort of an end condition. Why would we get a "out of stack space" error message if a recursive function does not have an end condition?
Answer: there is a limited amount of memory. each time a function is called, it is placed in the memory. If another is called, it is also added to the memory. In a recursive function, memory is continually taken up until the function loop is broken. This is called a “stack” -- instances of the function are being stacked up in the memory.
- Reflect about the difference between object inheritance and class inheritance
Answer: Inheritance is where an object inherits the qualities of something else. Object inheritance occurs when a new object is created and set equal to an existing object. That object then has all of the same characteristics as the original object. Class inheritance is different, in that when an object is set equal to a class, the object only refers to the characteristics of the class. This means that if the class changes, the object reference changes as well.
- What is object augmentation, and how do we do it?
Answer: an object holds sets of values -- a string, and a non type-specific variable. new sets can be added at any time using a simple assignment statement. If someone has an object “car”
with sets { “color”:”blue” , “year”:1997 } someone could add a set called “model” by saying car.model = “focus”;
- There is a way to add a method to String, such as any new String we create will have that augmented method (this is a bit different from object augmentation). How would you do this?
Answer: add the new method to String.prototype
- What is garbage collection?
Answer: garbage collection removes all unnecessary data from the memory. For example, once a for statement is finished running, it no longer needs the counter initialized from within that function. this value is removed from memory, helping to prevent memory overload and maintain efficiency
- What is the difference between an array and an object?
Answer: objects are unordered pairs of values -- a string to name the pair, and a non-type-specific variable. An array has an order and a length. Instead of having a string name, it is organized by its number, or space in the array. if you want to add something to an object, it does not effect the other pairs. however, if you want to add something to an array, you will need to slide the other pairs over to make room, or simply add it to the end of the array.
Homework:Ex. 3.1
Write a function called absolute, which returns the absolute value of the number it is given as its argument. The absolute value of a negative number is the positive version of that same number, and the absolute value of a positive number (or zero) is that number itself.
ANSWER:
function absVal (a) {
if (!a) return ("you did not enter an interger");
else if (a< 0) return (-1 * a);
else return a;
}
alert(absVal( (prompt("type an interger", ""))*1) );
Ex. 3.2
Write a function greaterThan, which takes one argument, a number, and returns a function that represents a test. When this returned function is called with a single number as argument, it returns a boolean: true if the given number is greater than the number that was used to create the test function, and false otherwise.
ANSWER:
function isGreaterThan(x) {
return function(y) {
return y > x;
};
}
var firstChoice = isGreaterThan(prompt("pick a number",""));