output diamond
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
// *
// ***
// *****
// *******
// *****
// ***
// *
// 层 1 2 3 4
// 空 3 2 1 0
// 星 1 3 5 7
// star = current * 2 - 1
// up_layer = math.floor(total_layer / 2) + 1
// space = up_layer - current
layer = parseInt(prompt("请输入层数"))
up_layer = Math.ceil(layer / 2)
for (let i = 1; i <= up_layer; i++){
str = ""
space_num = up_layer - i;
for(let j = 1;j <= space_num; j++){
str += " "
}
star_num = i * 2 -1
for(let j = 1; j <= star_num; j++){
str += "*"
}
console.log(str)
}
down_layer = up_layer - 1
for (let i = down_layer; i >= 1; i--){
str = ""
space_num = down_layer - i + 1;
for(let j = 1; j <= space_num;j++){
str += " ";
}
star_num = i * 2 -1
for(let j = 1; j <= star_num; j++){
str += "*";
}
console.log(str)
}
</script>
</body>
</html>
Find the number of all three-digit daffodils (the number of daffodils is: ones cube➕tens cube➕hundreds cube=the number itself)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
for (let i = 100; i <= 999; i++){
let ge = i % 10
let shi = parseInt(i/10) % 10
let bai = parseInt(i/100)
if (i == Math.pow(ge,3) + Math.pow(shi,3) + Math.pow(bai,3)){
console.log(i)
}
}
</script>
</body>
</html>
Find 10 with a loop!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
let sum = 1
for(let i = 1; i <= 10; i++){
sum *= i
}
console.log(sum)
</script>
</body>
</html>
Find 10 with a loop! +9! +8! ……1!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
let count = 0
let a = 1
for(let i = 1; i <= 10; i++){
a = a*i
count += a
}
console.log(count)
</script>
</body>
</html>
If you can only walk up one step at a time or jump up two steps at once. Now there are a total of N steps, please calculate how many ways there are from the 0th step to the Nth step
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<!-- 1.假如一次只能走上一阶或者一下子蹦上两阶。
现在一共有N阶台阶,请你计算一下从第0阶到第N阶共有几种走法。 -->
<script>
let steps = parseInt(prompt("请输入台阶数量:"))
console.log("一共有"+Climb(steps)+"种方法")
function Climb(n){
if(n <= 2){
return n
}
return Climb(n-1) + Climb(n-2)
}
</script>
</body>
</html>
A pair of rabbits can grow up in 4 months. After they grow up, they will give birth to a pair of rabbits every month. How many pairs of rabbits are there in the nth month?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
let m = parseInt(prompt("请输入月份数:"))
console.log("第"+m+"个月有"+Rabbit(m)+"只")
function Rabbit(n){
if(n <= 4){
return 1
}
return Rabbit(n-1)+Rabbit(n-3)
}
</script>
</body>
</html>