JavaScript第三天練習

空空777 2022-01-08 06:26:13 阅读数:889

javascript 第三天 第三

1. 直角三角形

<script>
for(var i = 0; i < 5; i++) {
for(var j = 0; j <= i; j++) {
document.write("*");
}
document.write("<br />");
}
</script>

2.九九乘法錶

<script>
document.write("<table cellspacing='9'>");
for(i = 1; i <= 9; i++) {
document.write("<tr>");
for(var j = 1; j <= i; j++) {
document.write('<td>' + j + "x" + i + "=" + i * j + "</td>");
}
document.write("</tr>");
}
document.write("</table>");
</script>

3.100-200之間第一個被7整除的

<script>
for(i = 100; i < 200; i++) {
if(i % 7 == 0) {
console.log(i);
break;
}
}
</script>

4.100-200之間所有奇數的和

<script>
var sum = 0;
var i = 100;
while(i >= 100 && i <= 200) {
if(i % 2 == 0) {
i++;
continue;
}
sum += i;
i++;
}
console.log("100-200之間所有奇數的和:" + sum);
</script>

5.   第十次斐波那契數列的數?

斐波那契數列:0,1,1,2,3,5,8,13,21...

<script>
var num1 = 1;
var num2 = 1;
var temp;
for(var i = 0; i <= 10; i++) {
temp = num2;
num2 = num1 + num2;
num1 = temp;
console.log(num2);
}
console.log(num2);
</script>

6.創建數組,跟倒序數組

<script>
var arr = [];
for(var i = 0; i < 10; i++) {
arr[i] = "第" + i + "個元素";
}
for(var i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
for(var i = arr.length - 1; i >= 0; i--) {
console.log(arr[i]);
}
</script>

7.數組

<script>
var arr1 = [10, 20, 30, 40, 50];
var sum = 0;
for(var i = 0; i < arr1.length; i++) {
sum += arr1[i];
}
console.log("求數組中的和:" + sum);
var arr2 = [60, 70, 80, 85, 96];
var sum2 = 0;
for(var i = 0; i < arr2.length; i++) {
sum2 += arr2[i];
}
console.log("數組的平均值:" + sum2 / arr2.length);
</script>

8.數組

<script>
var arr1 = [101,5,6,7,8,9,130,120,666,888,2,999];
var max = arr1[0];
for(var i = 0; i < arr1.length; i++) {
if(max < arr1[i]) {
max = arr1[i];
}
}
console.log("最大值是:" + max);
var arr2 = [2,100,101,10,101,5,6,7,8,9,130,120,666,888,1];
var min = arr2[0];
for(var i = 0; i < arr2.length; i++) {
if(min > arr2[i]) {
min = arr2[i];
}
}
console.log("最小值是:" + min);
</script>

9.數組昇級版

<script>
var arr = [20, 55, 66, 78, 96];
var sum = 0;
var max = arr[0];
var min = arr[0];
for(var i = 0; i < arr.length; i++) {
sum += arr[i];
if(max < arr[i]) {
max = arr[i];
} else if(min > arr[i]) {
min = arr[i];
}
}
console.log("數組中的和:" + sum);
console.log("數組中的最大值:" + max);
console.log("數組中最小值:" + min);
</script>

10.數組拼接字符

<script>
var names = ['迪奧', '香奈兒', '古馳', '嬌蘭', '蘭蔻', '楊樹林'];
var str = "";
for(var i = 1; i < names.length; i++) {
str += "|" + names[i];
}
console.log(names[0] + str);
</script>

版权声明:本文为[空空777]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201080626126448.html