Meeting 8
Introducing Objects
8.1 Homework Discussion
Maria: We’re quite excited ab out showing you our homework today because we came
up with several solutions and we’re not sure which is best.
Professor: Let me see.
Maria: Here’s the first one:
var a = 63, b = 98;
while (a != b) {
if (a > b) {
a -= b;
}
else {
b -= a;
}
}
document.write("GCD is " + a);
Professor: That’s a pretty forthright solution. I have one question, th ough. If a an d b
are equal, then the else part is executed and b bec omes zero . From here on, neither a
nor b can cha nge and you’re stuck inside an infinite loop. Have you thought o f that?
Mike: This cannot happen. If a and b are equal, then the while loop condition is
false and the if/else statement doesn’t even start.
Professor: Exactly. And what are yo ur other solutions?
Maria: We experimented a little with the DevTools and discovered a handy tool for
printing intermediate results. Just before the closing brace of the while loop we
inserted a console.log () function call, which we used to write the values of a and
b to the JavaScript Console within the DevTools:
147
...
else {
b -= a;
}
console.log("a=" + a + ", b=" + b);
}
document.write("GCD is " + a);
Inside th e JavaScript Console we got the following values.
Professor: Excellent. What you did is called tracing. Tracing is a way of record-
ing information about how a pro gram executes, and is typically used for debugging
purposes. Please, go on.
Mike: Inspecting the values, we discovered that the same subtraction is sometimes
repeated several times if the larger variable stays larger after the subtraction. We
hence thought the program could execute much faster if we rewrote it as follows:
var a = 63, b = 98;
while (a != b) {
while (a > b) {
a -= b;
}
while (a < b) {
b -= a;
}
}
document.write("GCD is " + a);
Professor: OK, I see your point. I do n’t think that there are actually fewer com-
parisons in this solution, though. You can try at home to count comparisons in both
progr ams, but you’ll have to do it for many dierent value combinations of a an d b
to get a general picture. Besides, speed is not something I would worry about in this
particular example.
What I like about this solution is that it makes what is actually go ing on mor e evident.
You can see right away th at one and the same variable is repeatedly being subtracted
until it becomes smaller.
148 Meeting 8. Introducing Objects
Maria: Exactly. Precisely this observation led us to the conclusion that we were
actually computing the remainder of a division. So we tried the following so lution:
var a = 63, b = 98;
while (a != b) {
if (a > b) {
a %= b;
}
if (a < b) {
b %= a;
}
}
document.write("GCD is " + a);
But u nfortunately it doesn’t work.
Professor: Let’s take one more look at the sequence of values pr oduced by your first
progr am. In the third line of the Console, the value of a is exactly four times that of b.
The statement a %= b; will therefore make a equal zero. Because a is now smaller
than b, the statement b %= a; is executed next. Since the remainder of division by
zero is not defined, b becomes NaN. End of story.
If you want to use the rem ainder operator, this is a possible solution:
var a = 63, b = 98;
var tmp;
while (b != 0) {
tmp = b;
b = a % b;
a = tmp;
}
document.write("GCD is " + a);
The trick is that you copy the old value of b to a with help fr om a temporary variable
tmp. Meanwhile, you use the old value of a to co mpute the remainder and assign it to
b. If you do that, a is always greater than b at the end of each iteration. Notice also
the changed loop condition, which now com pares b to zero.
Interesting in this last solu tion is that the first iteration of while just swaps the values
of both variables if the run starts o with b greater than a.
Mike: By the way, we also extended the multiplication pr ogram you gave us on
page 135 to work with negative integers. First we computed the sign of the prod-
uct and absolute values of multiplicands, and in the end we multiplied the product
with the sign:
var product = 0, x = 6, y = 8;
var sign = x * y >= 0 ? 1 : -1;
x = x >= 0 ? x : -x;
y = y >= 0 ? y : -y;
8.1. Homework Discussion 149
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.225.220