-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
108 lines (79 loc) · 1.86 KB
/
Copy pathfunctions.js
File metadata and controls
108 lines (79 loc) · 1.86 KB
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
var x = "outside";
var f1 = function() {
var x = "inside f1";
};
f1();
console.log(x); // logs 'outside'
var f2 = function() {
x = "inside f2";
};
f2();
console.log(x); // logs 'inside f2'
/*
closure - closes over local vars to enable reference to certain states
*/
function multipler(factor) { // input: number; output: function
return function(number) { // input: number; output: returns number
return number * factor;
};
}
var twice = multipler(2);
console.log(twice(5));
/*
recursion
*/
function power(base, exponent) {
if ( exponent === 0 ) {
return 1;
} else {
return base * power(base, exponent-1);
}
}
console.log(power(2,3));
/*
exercise: minimum
takes two args, returns smallest
*/
function min(x, y) {
if (x > y) return y;
else return x;
}
console.log(min(40,9));
/*
exercise: recursion
1. zero is even
2. one is odd
3. for any other number N, its evenness is the same as N-2
isEven() should correspond to this description; should return boolean
fix it so that it can deal with -1
*/
function isEven(number) {
if (number === 0) return true;
else if (number === 1) return false;
else if (number < 0) return isEven(0 - number) //deals with negative integers
else return isEven(number-2);
}
var result = isEven(-2) == true;
console.log(result);
/*
exercise: bean counting
"string".charAt(N) returns the Nth character of the string
N is zero-indexed
"string".length
countBs(string) should return number of B's in string (case sensitive)
countChar(string, char) generalize given any character in string
*/
function countChar(string, char) {
var count = 0;
for (var i = 0; i < string.length; i++) {
if (string.charAt(i) === char) {
count += 1;
}
}
return count;
}
console.log(countChar('Blue Bl Bastard', 'r'));
function countBs(string) {
return countChar(string, "B");
}
console.log(countBs('Blue Bastard'));