-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-EJS-Ch3-exercises.js
109 lines (80 loc) · 2.79 KB
/
06-EJS-Ch3-exercises.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var assert = require('assert');
var ___ = undefined;
/*
* To complete the exercise, replace underscores in the
* code below with appropriate values to make the tests
* pass.
*
* See README.md for installing the test library and
* running the tests.
*/
/*
## Minimum
The previous chapter introduced the standard function Math.min
that returns its smallest argument. We can do that ourselves
now. Write a function min that takes two arguments and returns
their minimum.
*/
// your code here
describe("The 'min' function", function(){
it("will find the minimum of two numbers", function(){
assert.strictEqual(min(0,10), 0);
assert.strictEqual(min(0,-10), -10);
});
});
/*
## Recursion
We’ve seen that % (the remainder operator) can be used to test
whether a number is even or odd by using % 2 to check whether
it’s divisible by two. Here’s another way to define whether a
positive whole number is even or odd:
Zero is even.
One is odd.
For any other number N, its evenness is the same as N - 2.
Define a recursive function isEven corresponding to this
description. The function should accept a number parameter and
return a Boolean.
Test it on 50 and 75. See how it behaves on -1. Why? Can you
think of a way to fix this?
*/
// your code here
describe("The 'isEven' function", function(){
it("can figure out even numbers", function(){
assert.strictEqual(isEven(50), true);
});
it("can figure out odd numbers", function(){
assert.strictEqual(isEven(75), false);
});
it("can handle negative numbers",function(){
assert.strictEqual(isEven(-1), false);
assert.strictEqual(isEven(-4), true);
});
});
/*
Bean counting
You can get the Nth character, or letter, from a string by
writing "string".charAt(N), similar to how you get its length
with "s".length. The returned value will be a string containing
only one character (for example, "b"). The first character has
position zero, which causes the last one to be found at position
string.length - 1. In other words, a two-character string has
length 2, and its characters have positions 0 and 1.
Write a function countBs that takes a string as its only argument
and returns a number that indicates how many uppercase “B”
characters are in the string.
Next, write a function called countChar that behaves like countBs,
except it takes a second argument that indicates the character
that is to be counted (rather than counting only uppercase “B”
characters). Rewrite countBs to make use of this new function.
*/
// your code here
describe("The 'countBs' function", function(){
it("correctly counts Bs",function(){
assert.strictEqual(countBs("BBC"), 2);
});
})
describe("The 'countChar' function", function(){
it("correctly counts ks",function(){
assert.strictEqual(countChar("kakkerlak", "k"), 4);
});
})