Monday, January 31, 2011

Homework - week 1 (track 1)

JavaScript 101: Homework -- Week 1

  1. The alliance of Netscape and Sun Microsystems did not use Java in the browser, because it was not suitable for that purpose. Why do you think Java was not suitable to be embedded in a browser?


Answer: Java is run as an executable, while JavaScript is run through a browser as text. This means that javaScript is easily added to the body of HTML -- which is also read via the browser as text.

  1. When parsing a String into a number using the parseInt() method, we are advised to always provide the radix. Why is it so strongly recommended?


Answer: The Radix argument tells the parsInt function which base to use for the returned variable. This is important because text might mean one thing, but is interpreted by the computer as another. for example, if there is a string that is meant to be hexidecimal, the parsInt funtion will only return the first numbers before any letters appear. If the radix is set as 16, this string will then properly return the value.

  1. What is a type, and why do you think types are useful in writing programs?


Answer: Any value has a type. For example, var ex = 7; is type “Number” while var ex = “7” is type “String” -- this is an important distinction because one program may require a number value -- if the type is tested, one can insure that this value is a number and not another incompattible type, like a boolean.

  1. Why do we lose precision when performing operations with decimal numbers in Javascript? Can you think of a few implications of why this would be a problem?


Answer: JavaScript only has 64 bits of precision. this means that when a number is anything but a whole positive integer, extra bits are used to indicate things like a negative integer and where the decimal place falls within the number. This means that only 52 bits are left for the digits in a number.

  1. Do you understand why the following operation produces the given result 115 * 4 - 4 + 88 / 2 = 500


Answer: JavaScript uses order of operations to determine what should be calculated first. In this case, multiplication and division are calculated first. The language is essentially adding in parenthesise into the equation. If parenthesise were added, the equation looks more like this:

(115 * 4) - 4 + (88 / 2) = 500

this simplifies to:

460 - 4 + 44 = 500

  1. What does typeof 4.5 do, and why does typeof (typeof 4.5) return "string" ?


Answer: the typeof is a function that returns a string. this string gives you the type of a particular value. This means that when typeof takes a number value, a string is returned that says “number.” Similarly, if a string value is given, typeof returns the string “string.” Because typeof always returns a string value, “typeof (typeof x)” will always return the string value “string.”


Ex. 2.1:

((4 >= 6) || ("grass" != "green")) &&
!(((
12 * 2) == 144) && true)

Is this true?


Answer:
= (false || True) && !(false && true)
= (true) && !(false)
= true && true

= true


Ex. 2.2:
Use the techniques shown so far to write a program that calculates and shows the value of 210 (2 to the 10th power).

Answer:
//Code:
var base = 2, power = 10, answer = 1, count = 0;

while (count <>

{

answer *= base;
count += 1;

}


alert (answer);

//Output: 2024

/* alternative:
var base = 2, power = 10;

alert(math.pow(base,power));

//This uses the Math object, with function “pow”
*/

Ex. 2.3:
With some slight modifications, the solution to the previous exercise can be made to draw a triangle. And when I say 'draw a triangle' I mean 'print out some text that almost looks like a triangle when you squint'.
Print out ten lines. On the first line there is one '#' character. On the second there are two. And so on.
How does one get a string with X '#' characters in it? One way is to build it every time it is needed with an 'inner loop' ― a loop inside a loop. A simpler way is to reuse the string that the previous iteration of the loop used, and add one character to it.

var count = 0; numLines = 10;
line = ""; output = "";

while (count <>
{
line += "#";
document.write(line);
count += 1;
}
/*output:
#
##
###
####
#####
######
#######
########
#########
##########
*/

Ex. 2.4:
Rewrite the solutions of the previous two exercises to use for instead of while.

Answer: a)

var base = 2, power = 10, answer = 1;

for (var x = 0; X <>

answer *= base;

alert (answer);

---------------

b)

var numLines = 10;

line = ""; output = "";


for (var x = 0; x <>

{

line += "#";
document.write(line);

}


Ex. 2.5:
Write a program to ask yourself, using prompt, what the value of 2 + 2 is. If the answer is "4", use alert to say something praising. If it is "3" or "5", say "Almost!". In other cases, say something mean.

Answer:
answer = prompt ("what is 2+2","????");
if (answer == "4")
alert ("yay! you aren't dumb!");

else if (answer == "3" || answer == "5")
alert ("almost... you're kinda dumb though...");

else
alert ("you are OFFICIALLY dumb!");

Ex. 2.6:
Add a while and optionally a break to your solution for the previous exercise, so that it keeps repeating the question until a correct answer is given.

Answer:
correctAns = false;
var answer;

while (!correctAns)
{
answer = prompt ("what is 2+2","????");
if (answer == "4")
{
correctAns = true;
alert ("yay! you aren't dumb!");
}

else if (answer == "3" || answer == "5")
alert ("almost... you're kinda dumb though...");

else
alert ("you are OFFICIALLY dumb!");

}


**Create an example that shows a valid use of the 'guard' and 'default' operators in Javascript.

Answer:

//default = ||

alert ((prompt("pick a number") || "you did not pick a number"));
/* if the person picks a number, it is displayed. If they dont, the message “you did not pick a number” is displayed */

//guard = &&

a = prompt("guess the right number");
alert (a && 5);
/* you are prompted to “guess the right number.” If your number is wrong, it will display the correct number. If its right, it will display the correct number.

2 comments:

  1. So, you asked for comments on Default and Guard operators. I'll take a swing at Default, and maybe we'll both understand it a little better. You wrote: alert ((prompt("pick a number") || "you did not pick a number")); as your example. First thing, the default value should BE a value. What I mean is, if the user doesn't choose a number, then your program will use the default number instead. I think... here's a link to my attempt to use Default with your "pick a number" example on jsfiddle.com Hope that's some help.

    ReplyDelete
  2. Oops. I just realized you only asked about Guard. Sorry! lol

    ReplyDelete