-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter_3_function.html
More file actions
77 lines (68 loc) · 2.41 KB
/
chapter_3_function.html
File metadata and controls
77 lines (68 loc) · 2.41 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>function</title>
<script type="text/javascript">
window.onload = function(){
// 返回两个数中的较小的一个
function min(a, b){
return a > b ? b:a;
};
// 用递归来判断任何数字 N 的奇偶性和 N-2相同
function isEven(num){
if(num < 0){
console.log("请输入正数");
return;
}
if(num == 0){
console.log("这是偶数");
return;
}
if(num == 1){
console.log("这是奇数");
return;
}
return isEven(num - 2);
};
// 写一个函数,统计给定字符串中的某个字符的个数,并返回这个数
function countChar(str, char){
var count = 0;
for(var i = 0; i < str.length; i++){
if(str.charAt(i) == char){
count++;
}
};
return count;
};
var btn1 = document.getElementById("3.13.1");
btn1.onclick = function(){
var res = min(5,3);
console.log(res);
};
var btn2 = document.getElementById("3.13.2");
btn2.onclick = function(){
isEven(520);
};
var btn3 = document.getElementById("3.13.3");
btn3.onclick = function(){
var str = document.getElementById("str");
var char = document.getElementById("char");
var res = document.getElementById("res");
res.value = char.value + ":" + countChar(str.value, char.value);
console.log(res.value);
};
};
</script>
</head>
<body>
<button id="3.13.1">最小值</button>
<button id="3.13.2">判断奇数还是偶数</button>
<button id="3.13.3">计算字符串中的某个字符的数量</button>
<input type="text" id="str" value="">
<input type="text" id="char" value="">
<input type="text" id="res" value="">
</body>
</html>