-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscript.js
More file actions
151 lines (118 loc) · 2.91 KB
/
Copy pathtranscript.js
File metadata and controls
151 lines (118 loc) · 2.91 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// You can find the transcript of the previous session here: http://codegirls.de/eloquentjs/05/transcript.html
// Exercise: Bean counting - http://eloquentjavascript.net/03_functions.html#h_3rsiDgC2do
var exampleWord = "Peter Pan";
console.log(exampleWord.charAt(0));
console.log(exampleWord.charAt(1));
console.log(exampleWord.charAt(2));
console.log(exampleWord.charAt(8));
console.log(exampleWord.length);
function countBs(word) {
var count = 0;
for (var i = 0; i < word.length; i += 1) {
// console.log(word.charAt(i));
if (word.charAt(i) == "B")
count += 1;
}
return count;
}
console.log(countBs("Peter B. Pan"))
//-> 1
function countChar(word) {
var count = 0;
for (var i = 0; i < word.length; i += 1) {
// console.log(word.charAt(i));
if (word.charAt(i) == "B")
count += 1;
}
return count;
}
console.log(countBs("BBC"));
//-> 2
// printChars("Peter Pan");
function countBs(word) {
// walk through word (by "counting" up to the length of word)
// for each character, check if it is a b
// if it is, count up
// return how many Bs we saw
}
// Exercise: Recursion - http://eloquentjavascript.net/03_functions.html#h_jxl1p970Fy
function isEven(x) {
if (x < 0) {
x = -x; // dreht das Vorzeichen um
// x = Math.abs(x); // das hier auch (abs = "absolut")
}
if (x == 0)
return true;
else if (x == 1)
return false;
else
return isEven(x - 2);
}
console.log(isEven(10))
//-> true
function isOdd(x) {
if (x < 0) {
x = -x; // dreht das Vorzeichen um
// x = Math.abs(x); // das hier auch (abs = "absolut")
}
if (x == 0)
return false;
else if (x == 1)
return true;
else
return isOdd(x - 2);
}
console.log(isOdd(10))
//-> False
//-> kürzer:
function isOdd(x) {
return !isEven(x);
}
// "Cheating" :)
function isEven(number) {
if (number%2 == 0) {
return true;
}
else {
return false;
}
}
function isEven(number) {
return number % 2 == 0;
}
// similar to http://eloquentjavascript.net/03_functions.html#h_jxl1p970Fy
// (i.e. take another look at the power function, maybe it inspires you ...)
// Exercise: Minimum - http://eloquentjavascript.net/03_functions.html#h_XTmO7z7MPq
function min (x,y) {
if (x < y)
return x;
if (x > y)
return y;
if (x == y)
return("beide gleich groß");
}
console.log(min(10, 10))
console.log(min(-2, 0))
function min(x, y) {
if (x < y)
return x;
else
return y;
}
function min(x, y) {
return x < y ? x : y;
}
console.log(Math.min(1,2,3,4,5))
//--> 1
function isEven(no){
while(no >= 2){
no = no-2;
}
if(no == 1){
result = false;
}
else if(no == 0){
result = true;
}
return result;
}