// 求最大值 <script> var arr = [10,35,765,21345,678,89]; var max = arr [0]; for (var i=0;i< arr.length;i++) { if (max<arr[i]){ max = arr [i]; } } console. ...
// =================== 求最大值=====================================
<script>
var arr = [10,35,765,21345,678,89];
var max = arr [0];
for (var i=0;i< arr.length;i++) {
if (max<arr[i]){
max = arr [i];
}
}
console.log("最大值:" + max);
</script>
// =================== 求最小值=====================================
<script>
var arr = [10,35,765,21345,678,89];
var max = arr [0];
for (var i=0;i< arr.length;i++) {
if (max>arr[i]){
max = arr [i];
}
}
console.log("最小值:" + max);
</script>
// ============================倒序遍曆數組===============================
<script>
var arr = [1,2,3,4,5,6,7,8,9];
for (var i= arr.length-1;i>=0;i--){
console.log(arr[i]);
}
</script>
//===========數組拼接字元串===========第一種方法(從前面往後加)=========
<script>
var names = ["周傑倫","李連傑","林俊傑","張韶涵","田馥甄"];
var str = "";
for (var i = 0; i< names.length-1;i++){
str += names[i] + "|"
}
console.log(str+ names[names.length-1]);
</script>
//===========數組拼接字元串===========第二種方法(從後面往前加)=========
<script>
var names = ["周傑倫","李連傑","林俊傑","張韶涵","田馥甄"];
var str = "";
for (var i = 1; i< names.length;i++){
str += "|" + names[i]
}
console.log(names[0]+ str);
</script>
// ==============去掉數組中重覆的0,把其他的數據放在一個數組中============
<script>
var arr = [10,20,0,12,0,0,124,32];
var newArr = [];
for (var i=0; i<arr.length; i++){
if (arr[i] != 0){
newArr[newArr.length] = arr[i];
}
}
console.log(newArr);
</script>
// =====================反轉數組----------把數組中的數據位置調換=============
<script>
var array = [10,20,30,40,50,60];
for (var i=0 ;i<=array.length/2;i++){
var temp = array [i];
array[i] = array[array.length-1-i];
array[array.length-1-i] = temp;
}
console.log(array);
</script>
//===========數列求成績,總成績,平均分,最大最小值===============//
<script>
var proInput = parseInt(prompt("請輸入計研17的班級人數"));
var proScore = [];
for (var i = 0; i< proInput; i++){
proScore[proScore.length] = parseInt(prompt("請輸入第"+ (i+1) + "個同學的成績"));
}
console.log(proScore);
var sum =0;
var aver = 0;
var max = proScore[0];
var min = proScore[0];
for (var i = 0;i< proScore.length;i++){
sum += proScore[i];
aver = sum/proScore.length;
if (max<proScore[i]){
max = proScore[i];
}
if (min>proScore[i]){
min = proScore[i];
}
}
console.log("班級總成績:"+ sum);
console.log("平均值:"+ aver);
console.log("最大值:"+ max);
console.log("最小值:"+ min);
</script>