Skip to main content

Ivan Teoh

Something personal yet public

Javascript: 101 Week 1 Track 2

This post is a reply for the Javascript: 101 Week 1 Track 2. Mainly answering questions for reflection and homework.

Reflection

  • Type is a way to represent the data in computer memory. It is useful in writing programs as it can be keep in the way as we want efficiently.
  • We lose precision when performing operations with decimal numbers in JavaScript because of the limitation of number representation in computer memory. It can be a problem when we are dealing money by dividing with odd number, such as 3.
  • The following operation produces the given result 115 * 4 - 4 + 88 / 2 = 500 as multiplication and division are calculating first before addition and subtraction. After multiplication and division, the equation will be 460 - 4 + 44.
  • A backslash character in a String is to indicate the next character is special, escape character.
  • The purpose of typeof 4.5 is to returning the type of 4.5 in string format, which is number in this case. On the other hand, typeof (typeof 4.5) will return string because typeof function is returning string type. There is not special type of data type in JavaScript.
  • The variables that might be created in the browsers environment when it loads a page with JavaScript in it are document and window.
  • Dear grandma, values are people, variables are house, and control flow is road that moving people from one house to another house.
  • Functions are groups of code that can be used whenever we want. We need them whenever we want to use it in many places. It is to reduce man made mistake and our life easy.

Homework

Exercises 2.1

164-exercise21.js (Source)

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

It is true. As (false || true) && !(false && true).

Exercises 2.2

Write a program that calculates and shows the value of 210 (2 to the 10th power).

164-exercise22.js (Source)

var total = 2;
var count = 1;
while (count < 10) {
    total = total * 2;
        count = count + 1;
}
alert(total);

Exercises 2.3

With some slight modifications, the solution to the previous exercise can be made to draw a triangle.

164-exercise23.js (Source)

var triangle = "";
var line = 0;
while (line < 10) {
    var count = 0;
        line = line + 1;
        hash = "";
        while (count < line) {
            hash = hash + "#";
            count = count + 1;
        }
        triangle = triangle + hash + '\n';
}
alert(triangle);

Exercises 2.4

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

From Exercises 2.3

164-exercise24a.js (Source)

for (var total = 2, count = 1; count < 10; count++) {
    total = total * 2;
}
alert(total);

From Exercises 2.4

164-exercise24b.js (Source)

for (var line = 0, triangle = ""; line < 10; line++) {
    for (var count = 0, hash = ""; count <= line; count++) {
        hash = hash + "#";
    }
    triangle = triangle + hash + '\n';
}
alert(triangle);

Exercises 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.

164-exercise25.js (Source)

var theNumber = prompt('What the value of 2 + 2 is?', '');
if (theNumber == '4')
    alert('Correct! Well done.');
else if (theNumber == '3' || theNumber == '5')
    alert('Almost!');
else
    alert('Ops! Please try again!');

Exercises 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.

164-exercise26.js (Source)

while (true) {
    var theNumber = prompt('What the value of 2 + 2 is?', '');
    if (theNumber == '4') {
        alert('Correct! Well done.');
        break;
    }
    else if (theNumber == '3' || theNumber == '5')
        alert('Almost!');
    else
        alert('Ops! Please try again!');
}

Exercises 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.

164-exercise31.js (Source)

function absolute(number) {
    if (number < 0)
        return -number;
    return number;
}

Exercises 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.

164-exercise32.js (Source)

function greaterThan(number1) {
    function test(number2) {
        return number2 > number1;
    }
    return test;
}

var greaterThanTwenty = greaterThan(20);
alert(greaterThanTwenty(52));
alert(greaterThanTwenty(10));

Comments

Comments powered by Disqus