Here is a collection of exercises usable for homework assignments, in class discussions, or exams.
|
|
1 - 2 - (3 - 4 - 5) - 6
! x && y && z
a < b > c == d > e
typeof + 3
first-guess a legal variable name in JavaScript? If not, why do you think such a
name was banned? (Answer using complete sentences. Back up your answer. Concrete examples will
most likely be helpful.)
var total = 1000;
var year = 0;
while (total < 5000) {
year = year + 1;
total = total * 1.05;
}
alert("It will take you " + year + " years to get there.");
Modify the script so that it first prompts for the APR and then computes the amount of time required to grow the $1000 to $5000 where the rate is compounded monthly. You may alert the result in either months or years.
indexOf operator on strings. Recall that s.indexof(t) will
return the first position (starting at 0) at which t appears, but returns a negative
number if t is not found within s.
"dog".charAt(2).charAt(0) and explain the result.
var capitals = {ME: "Augusta", NH: "Concord", VT: "Montpelier", MA: "Boston",
CT: "Hartford", RI: "Providence"};
Therefore, if your user inputs NH, your script should alert Concord. If you want a point
of extra credit, allow the user to enter data in any case, for example NH, Nh, nH, or nh for
New Hampshire.
var p1 = {name: "Alice"};
var p2 = {name: "Bob", manager: p1};
p1.manager = p1;
What is the value of p2.manager.manager.manager.name?
a after executing
the following:
var a = [1,2,3,4]; a.unshift(a.pop()); a.concat([5, 6, 7]);
var character = {real: true, gender: "female"};
var alice = Object.create(character);
alice.name = "Alice";
alice.next = Object.create(character);
alice.next.name = "Tweedledee";
alice.next.real = false;
alice.next.gender = "male";
alice.next.next = Object.create(character);
alice.next.next.real = false;
alice.next.next.name = "Red Queen";
alice.next.next.next = alice;
"" ⇒ ""
"it" ⇒ "ti"
"lizard" ⇒ "ardliz"
"this laptop is lame" ⇒ "op is lamethis lapt"
dollarMessage
that has one parameter representing a number of dollars and returns
a string stating how many dollars this is. Here are some test cases:
dollarMessage(5) ⇒ "5 dollars"
dollarMessage(1) ⇒ "1 dollar"
dollarMessage(7.244) ⇒ "7.244 dollars"
Use a conditional expression, not an if-statement, to distinguish
between writing "dollar" versus "dollars".
isNonnegativeAndFinite that
takes a single parameter and returns whether or not its value,
when interpreted as a number, is nonnegative and finite.
sign that takes one parameter, a
number, and returns -1 if the number is negative, 0 if the number is 0,
1 if the number is positive, and NaN if the parameter is not a number.
Use an if-statement.
isSmallInteger which returns true
if its sole parameter is an integer between -128 and 127, inclusive, and false
otherwise. Your function body should have a single return statement in which you
test that your parameter is not NaN, not a floating point number (you can use
the remainder when divided by 1 idiom), greater than or equal to -128 and less than
or equal to 127.
isSmallInteger("dog") ⇒ false
isSmallInteger(-129) ⇒ false
isSmallInteger(-128) ⇒ true
isSmallInteger(127) ⇒ true
isSmallInteger(128) ⇒ false
isSmallInteger(14.8) ⇒ false
isSmallInteger(14.0) ⇒ true
sumOfRange that takes in two parameters,
x and y, which should both be small integers (see the previous
problem), and returns the sum of all of the integers in the range from x
(inclusive) to y (exclusive). If either of x or y is
not a small integer (call the function from the previous problem to check this!!!) then
throw an exception. Example test cases for you:
sumOfRange(5, 9) ⇒ 5 + 6 + 7 + 8 = 26
sumOfRange(-2, 4) ⇒ -2 + -1 + 0 + 1 + 2 + 3 = 3
sumOfRange(9, 5) ⇒ 0 (because there are no numbers >=9 AND <5)
sumOfRange(-99999, 0) ⇒ throws an exception
sumOfRange(14, "dog") ⇒ throws an exception
sumOfEvensInRange that takes in two parameters,
x and y, which should both be short integers (see the previous problem), and returns the
sum of all of the even integers in the range from x (inclusive) to y (exclusive).
If either of x or y is not a short integer (call the function from the previous
problem to check this!!!) then throw an exception. Example test cases for you:
sumOfEvensInRange(5, 9) ⇒ 6 + 8 = 14
sumOfEvensInRange(-2, 4) ⇒ -2 + 0 + 2 = 0
sumOfEvensInRange(9, 5) ⇒ 0 (because there are no numbers >=9 AND <5)
sumOfEvensInRange(-99999, 0) ⇒ throws an exception
sumOfEvensInRange(14, "dog") ⇒ throws an exception
sumOfCodepoints which takes a string and returns
the sum of each of the codepoints in the string.
withoutMiddleThird which takes a string and returns
the string that is like its argument but without its "middle third"
characters. Examples:
withoutMiddleThird("ABCDEFGHI") ⇒ "ABCGHI"
withoutMiddleThird("") ⇒ ""
withoutMiddleThird("a") ⇒ "a"
withoutMiddleThird("ab") ⇒ "b"
withoutMiddleThird("abc") ⇒ "ac"
squares that takes in an array
and returns a NEW array containing all the squares of the input array.
Test cases:
squares([1,7,-4]) ⇒ [1,49,16]
squares([]) ⇒ []
squares([true, -6, null]) ⇒ [1, 36, 0]
square that takes in an array of
numbers and CHANGES THIS ARRAY by replacing each element with its
square. Test cases:
var a = [1,7,-4];
square(a);
a ⇒ [1,49,16]
chomped that takes in an array
and returns a NEW array containing all the values of the input array,
interpreted as strings, with their last characters removed.
Test cases:
chomped(["one", "two", "three"]) ⇒ ["on", "tw", "thre"]
chomped([]) ⇒ []
chomped([true, -6, null]) ⇒ ["tru", "-", "nul"]
chompAll that takes in an array
and CHANGES THIS ARRAY by replacing each element with its
chompedness. The description of chomping can be gleaned from reading
the description in the previous problem. Test cases:
var a = ["one", "two", "three"];
chompAll(a);
a ⇒ ["on", "tw", "thre"]
halfsies(1) ⇒ 0
halfsies(0) ⇒ 0
halfsies(-22.7) ⇒ 0
halfsies(2) ⇒ 1
halfsies(19) ⇒ 5
halfsies(7.225E102) ⇒ 342
var product = 1;
// Returns the factorial of n. Precondition: n is a positive
// integer <= 18.
var factorial = function (n) {
for (var i = 2; i <= n; i += 1) {
product *= i;
}
return product;
}
Naturally, you are extremely disappointed in your co-worker's solution. Why is it so bad?
(For full credit, don't just say "Because it doesn't work." Give an explanation that shows
you understand the defect in the code.)
var x = 20;
var f = function (y) {alert(x / y); y = 3;}
f(8);
alert(y);
var sum;
for (int i = 1; i < 10000; i++) {
sum += i;
alert("The sum from 1 to 10000 is " + sum);
}
prompt, then displays the Fuddified version of the
string with alert. (The Fuddified version of a string
is the string with all r's and l's replaced with w's.)
lengthOfCollatzSequence(1) ⇒ 1
lengthOfCollatzSequence(3) ⇒ 8
lengthOfCollatzSequence(4) ⇒ 3
lengthOfCollatzSequence(16) ⇒ 5
isLeapYear(2000) ⇒ true
isLeapYear(1940) ⇒ true
isLeapYear(2100) ⇒ false
isLeapYear(1987) ⇒ false
isLeapYear(1381) ⇒ (throws an exception)
stripVowels("Hello, world") ⇒ "Hll, wrld"
scramble("Hello, world") ⇒ "w,dlroH elol"
powersOfTwo(70) ⇒ [1,2,4,8,16,32,64]
var x = 1;
var f = function (y) {alert(x + y);}
f(2);
What would happen if the parameter y were renamed to x?
stutter([5,true,[3],"ha"]) ⇒ [5,5,true,true,[3],[3],"ha","ha"]
stutter([{}, null]) ⇒ [{}, {}, null, null]
prefixes("eich") ⇒ ["", "e", "ei", "eic", "eich"]
prefixes("") ⇒ [""]
suffixes("eich") ⇒ ["", "h", "ch", "ich", "eich"]
suffixes("") ⇒ [""]
sift("javascript", 2) ⇒ "aacit"
sift("python", -4) ⇒ throws an exception
sift("coffeescript", 5) ⇒ "offescrit"
sift("java", alert) ⇒ throws an exception
powers(3, 400) ⇒ [1,3,9,27,81,243]
powers(-2, 17) ⇒ throws an exception
powers(3, -9) ⇒ []
powers(0.5, 22) ⇒ throws an exception
interleave(["a", "b"], [1, 2, true, nil]) ⇒ ["a", 1, "b", 2, true, nil]
interleave(["a", "b"], []) ⇒ ["a", "b"]
objectify("a,dog|b,cat|z,rat") ⇒ {a: "dog", b: "cat", z: "rat"}
objectify("") ⇒ {}
objectify("one,uno|two,dos") ⇒ {one: "uno", two: "dos"}
var swapFirstTwoElementsOf = function (a) {
var oldFirstElement = a[0];
a[0] = a[1];
a[1] = oldFirstElement;
}
This works. Then your friend comes along and says "oh hang on, I can do
better" and writes this:
var swapFirstTwoElementsOf = function (a) {
a = [a[1],a[0]].concat(a.slice(2, a.length));
}
Does your friend's function work? Why or why not? Be very specific.
Use precise, technical language.
var x = 2;
var f = function () {return this.x - 5;}
var p = {x: 1, y: x, z: f};
alert(f === p.z);
alert(f());
alert(p.z());
alert(p.y);
take which takes
an array a and a number n and returns a new
array consisting of the first n elements of a. If
n is greater than the length of a, return a copy
of a.drop which takes
an array a and a number n and returns a new
array consisting of all but the first n elements of a. If
n is greater than the length of a, return an empty
array.
first([4,3,9,2,10], function (x) {return x > 5}) ⇒ 9
first([Infinity, "dog", 8, NaN], isFinite) ⇒ 8