study/html/斐波那契数列生成器.html

40 lines
1.3 KiB
HTML
Raw Permalink Normal View History

2022-08-04 04:30:02 +00:00
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="" />
<title>斐波那契数列生成器</title>
</head>
<body>
<h1>斐波那契数列生成器</h1>
<h2>作者:赵海洋</h2>
<p>斐波那契数列由0和1开始之后的斐波那契数就是由之前的两数相加而得出。</p>
<div>
<input type="text" id="number" placeholder="输入一个正整数" />
<button onclick="go()">生成斐波那契数列</button>
</div>
<p><code id="seq"></code></p>
<script>
// 斐波那契数列生成器
function* fibonacci(max = 1000) {
let current = 0
let next = 1
while (current < max) {
yield current
let prev = current
current = next
next += prev
}
}
function go() {
if (number.value.match(/^\d+$/)) {
seq.innerText = [...fibonacci(number.value)].join(', ')
number.value = ''
} else alert('请输入一个正整数')
}
</script>
</body>
</html>