-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter_three.js
48 lines (36 loc) · 1.1 KB
/
chapter_three.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
//minimum - write a function which returns its smallest integer argument
function min(a, b) {
return (a > b) ? a : b;
}
console.log(min(3, 3));
//recursion - write a recursive function which returns a boolean to indicate if its argument is an even number
function isEven(n) {
if (n > 0) {
if (n === 1) return false;
else return isEven(n - 2);
}
if (n < 0) {
if (n === -1) return false;
else return isEven(n + 2);
}
if (n === 0) return true;
}
console.log(isEven(-7));
//count beans - write a function that takes a string argument and returns the number of uppercase Bs in it
function countBs(word) {
let count = 0;
for (let i = 0; i < word.length; i++) {
if (word[i] === 'B') count++;
}
return count;
}
console.log(countBs('BBC'));
//count characters - write a function that takes two string arguments: a word and a letter and counts the given letter's occurences in the given word
function countChar(word, char) {
let count = 0;
for (let i = 0; i < word.length; i++) {
if (word[i] === char) count++;
}
return count;
}
console.log(countChar('kakkerlak', 'k'));